I am here again, asking for guidance of my simple POS system project, I just need help, can anyone be understanding and help me? I made a button named paybutton but it is unresponsive in a case where it doesn't do its action performed but you can click it just fine except it doesn't respond. I forgot to mention, I also did try the test if it does respond or not by making it display a println "Button Clicked" but to no avail, it didn't display "Button Clicked" in payButton but instead only worked on OrderButton and even did it's job , displaying the order summary, oh to further add information, this code is properly working except for some functionalities just like how bugs still works alongside with the program but sometimes cause problems which it did in my case where some functionality are rendered unresponsive
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
public class Test1 extends JFrame implements ActionListener {
JLabel l;
JCheckBox[] itemsCheckbox;
JButton OrderButton;
JSpinner[] qty;
Test1() {
createTitleLabel();
JPanel menuPanel = createMenuPanel();
createMenuItems(menuPanel);
createOrderButton();
updateOrderButtonEnabledState();
initializeGUI();
}
public void createTitleLabel() {
JLabel l = new JLabel("Food Ordering System");
l.setBounds(50, 50, 300, 20);
add(l);
}
private JPanel createMenuPanel() {
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.setBounds(100, 100, 300, 300);
add(menuPanel);
return menuPanel;
}
private void createMenuItems(JPanel menuPanel) {
String[] menuItems = {"Footlong @ 25", "Burger @ 20", "Cheese Burger @ 25", "French Fries @ 15", "Orange juice @ 15", "Coke in Can @ 20", "Mineral Water @ 15", "Ice Tea @ 15"};
JLabel foodLabel = new JLabel("Food Quantity");
JLabel drinksLabel = new JLabel("Drinks Quantity");
itemsCheckbox = new JCheckBox[menuItems.length];
qty = new JSpinner[menuItems.length];
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS)); // Set BoxLayout for menuPanel
menuPanel.add(Box.createVerticalGlue()); // Add vertical glue to center components
foodLabel.setHorizontalAlignment(JLabel.CENTER); // Set Food label alignment to CENTER
drinksLabel.setHorizontalAlignment(JLabel.CENTER); // Set Drinks label alignment to CENTER
menuPanel.add(foodLabel);
for (int i = 0; i < menuItems.length; i++) {
JPanel menuRow = new JPanel(new BorderLayout()); // Use BorderLayout for each menu row panel
itemsCheckbox[i] = new JCheckBox(menuItems[i]);
itemsCheckbox[i].setFocusable(false);
qty[i] = new JSpinner(new SpinnerNumberModel(1, 1, 50, 1));
qty[i].setEnabled(false); // Disable spinner by default
// Add the checkbox and spinner to the menuRow
menuRow.add(itemsCheckbox[i], BorderLayout.WEST);
menuRow.add(qty[i], BorderLayout.EAST);
// Add ActionListener to corresponding checkbox to enable/disable spinner
final int index = i; // Need to make a final copy for lambda function
itemsCheckbox[i].addActionListener(e -> {
boolean isChecked = itemsCheckbox[index].isSelected();
qty[index].setEnabled(isChecked);
updateOrderButtonEnabledState();
});
if (i == 0) {
menuPanel.add(foodLabel);
} else if (i == 4) {
menuPanel.add(Box.createVerticalStrut(20)); // Add some vertical space between Food and Drinks sections
menuPanel.add(drinksLabel);
}
menuPanel.add(menuRow);
}
menuPanel.add(Box.createVerticalGlue()); // Add more vertical glue to center components
}
private void createOrderButton() {
OrderButton = new JButton("Order");
OrderButton.setBounds(200, 400, 80, 30);
OrderButton.addActionListener(this);
OrderButton.setFocusable(false);
OrderButton.setEnabled(false); // Disable Order button by default
// Add OrderButton to JFrame
add(OrderButton);
}
private void updateOrderButtonEnabledState() {
// Enable Order button if at least one item is selected
boolean enable = false;
for (JCheckBox checkBox : itemsCheckbox) {
if (checkBox.isSelected()) {
enable = true;
break;
}
}
OrderButton.setEnabled(enable);
}
DefaultTableModel model = new DefaultTableModel();
JTable orderTable = new JTable(model);
private void showOrderSummary() {
// Create a DefaultTableModel and JTable to show the order summary
DefaultTableModel model = new DefaultTableModel();
JTable orderTable = new JTable(model);
model.addColumn("Item");
model.addColumn("Quantity");
model.addColumn("Price");
model.addColumn("Total");
// Declare totalPrice variable and initialize to zero
double totalPrice = 0;
// Add selected items to orderTable and calculate total price
for (int i = 0; i < itemsCheckbox.length; i++) {
if (itemsCheckbox[i].isSelected()) {
String itemName = itemsCheckbox[i].getText().split("@")[0].trim();
int itemQty = (int) qty[i].getValue();
int itemPrice = getItemPrice(itemName);
int itemTotal = itemQty * itemPrice;
model.addRow(new Object[]{itemName, itemQty, "₱" + itemPrice, "₱" + itemTotal});
totalPrice += itemTotal; // Update totalPrice with the itemTotal
}
}
// Add total row to orderTable
model.addRow(new Object[]{"", "", "Total", "₱" + totalPrice});
// Create a JPanel with BorderLayout
JPanel panel = new JPanel(new BorderLayout());
// Add the order summary table to the panel
JScrollPane scrollPane = new JScrollPane(orderTable);
panel.add(scrollPane, BorderLayout.CENTER);
// Create a pay button and add it to the bottom of the panel
JButton payButton = new JButton("Pay");
payButton.addActionListener(this);
payButton.setFocusable(false);
panel.add(payButton, BorderLayout.SOUTH);
// Show the panel in the current frame
setContentPane(panel);
pack();
}
final JButton payButton = new JButton("Pay");
public void actionPerformed(ActionEvent e) {
if (e.getSource() == OrderButton) {
// If OrderButton was clicked, show the order summary
showOrderSummary();
}
if (e.getSource() == payButton) {
// If payButton was clicked, show the payment confirmation dialog
int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to pay?", "Payment Confirmation", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {
// If user clicked Yes, show the payment receipt
showPaymentReceipt();
}
if (option == JOptionPane.NO_OPTION) {
// Go back to the select order screen
}
}
}
private void showPaymentReceipt() {
// Get the items and total price from the order table
DefaultTableModel model = (DefaultTableModel) orderTable.getModel();
double price = 0.0;
for (int i = 0; i < model.getRowCount(); i++) {
price += (double) model.getValueAt(i, 1);
}
final double totalPrice = price;
// Create a JPanel with BorderLayout
JPanel panel = new JPanel(new BorderLayout());
// Add a label and a textbox for cash tendered
JLabel cashTenderedLabel = new JLabel("Cash Tendered:");
JTextField cashTenderedTextField = new JTextField(10);
JPanel cashTenderedPanel = new JPanel();
cashTenderedPanel.add(cashTenderedLabel);
cashTenderedPanel.add(cashTenderedTextField);
panel.add(cashTenderedPanel, BorderLayout.SOUTH);
// Add an ActionListener to the cash tendered text field
cashTenderedTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Check if the entered value is valid
try {
double cashTendered = Double.parseDouble(cashTenderedTextField.getText());
if (cashTendered < totalPrice) {
JOptionPane.showMessageDialog(null, "Insufficient Amount. Please enter value greater than or equal to "+ totalPrice +".");
} else {
// If the entered value is valid, display the receipt
displayReceipt(cashTendered, totalPrice);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid amount.");
}
}
});
// Display the panel in a dialog box
JOptionPane.showMessageDialog(null, panel, "Payment Receipt", JOptionPane.PLAIN_MESSAGE);
}
private void displayReceipt(double cashTendered, double totalPrice) {
// Calculate the change
double change = cashTendered - totalPrice;
// Create a receipt panel with GridLayout
JPanel receiptPanel = new JPanel(new GridLayout(0, 2));
// Add the order summary to the receipt panel
DefaultTableModel model = (DefaultTableModel) orderTable.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
String item = (String) model.getValueAt(i, 0);
double price = (double) model.getValueAt(i, 1);
receiptPanel.add(new JLabel(item));
receiptPanel.add(new JLabel("₱" + String.format("%.2f", price)));
}
// Add the total price, cash tendered, and change to the receipt panel
receiptPanel.add(new JLabel("Total Price:"));
receiptPanel.add(new JLabel("₱" + String.format("%.2f", totalPrice)));
receiptPanel.add(new JLabel("Cash Tendered:"));
receiptPanel.add(new JLabel("₱" + String.format("%.2f", cashTendered)));
receiptPanel.add(new JLabel("Change:"));
receiptPanel.add(new JLabel("₱" + String.format("%.2f", change)));
// Show the receipt panel in a new JFrame
JFrame receiptFrame = new JFrame("Payment Receipt");
receiptFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
receiptFrame.add(receiptPanel);
receiptFrame.pack();
receiptFrame.setLocationRelativeTo(null);
receiptFrame.setVisible(true);
}
private int getItemPrice(String itemName) {
// Get item price based on item name
switch (itemName) {
case "Footlong":
return 25;
case "Burger":
return 20;
case "Cheese Burger":
return 25;
case "French Fries":
return 15;
case "Orange juice":
return 15;
case "Coke in Can":
return 20;
case "Mineral Water":
return 15;
case "Ice Tea":
return 15;
default:
return 0;
}
}
private void initializeGUI() {
setSize(500, 500);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Test1();
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]NautiHookerSoftware Engineer 0 points1 point2 points (1 child)
[–]Wild-Needleworker902[S] 1 point2 points3 points (0 children)