So I have an Item that consist item name, jspinner value, price, item id and that item frame has a button.
The issue is when I click Add it doesnt add anything (I removed the actionlistener right now as I wanna start over again without its function as it has given me multiple errors and exceptions for a few hours now)
ps. im a freshman and i dont know what im doing but i get the gist of but ya our prof gave us a project to work with without teaching us how to code itself
please help me
package login;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class Ordering extends JFrame {
private JPanel mainPanel;
private JPanel drinksPanel;
private JPanel dessertsPanel;
private JPanel othersPanel;
private JButton tabbtn1;
private JButton tabbtn2;
private JButton tabbtn3;
private JButton tabbtn4;
private JButton resetbtn;
private JButton printbtn;
public Ordering() {
initializeUI();
}
private void initializeUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1600, 950);
setLayout(null);
setLocationRelativeTo(null);
// Tabs panel with left-aligned flow layout
JPanel tabs = new JPanel(new FlowLayout(FlowLayout.LEFT));
tabs.setBounds(0, 0, 900, 50);
tabs.setBackground(Color.blue);
add(tabs);
tabbtn1 = new JButton("All Categories");
tabbtn1.setPreferredSize(new Dimension(150, 40));
tabbtn1.addActionListener(e -> switchToMainPanel());
tabs.add(tabbtn1);
tabbtn2 = new JButton("Drinks");
tabbtn2.setPreferredSize(new Dimension(150, 40));
tabbtn2.addActionListener(e -> switchToDrinksPanel());
tabs.add(tabbtn2);
tabbtn3 = new JButton("Desserts");
tabbtn3.setPreferredSize(new Dimension(150, 40));
tabbtn3.addActionListener(e -> switchToDessertsPanel());
tabs.add(tabbtn3);
tabbtn4 = new JButton("Others");
tabbtn4.setPreferredSize(new Dimension(150, 40));
tabbtn4.addActionListener(e -> switchToOthersPanel());
tabs.add(tabbtn4);
// Main panel
mainPanel = createMainPanel();
add(mainPanel);
// Drinks panel
drinksPanel = createPanel(Color.yellow);
// Group 9: Item 1
ItemPanel itemPanel9 = new ItemPanel("D1", "Coke", 20.00, "C:\Users\PC\Desktop\icons\9.jpg");
itemPanel9.setBounds(33, 18, 140, 190);
drinksPanel.add(itemPanel9);
// Group 10: Item 1
ItemPanel itemPanel10 = new ItemPanel("D2", "Sprite", 20.00, "C:\Users\PC\Desktop\icons\10.jpg");
itemPanel10.setBounds(206, 18, 140, 190);
drinksPanel.add(itemPanel10);
// Group 6: Item 1
ItemPanel itemPanel11 = new ItemPanel("D3", "Royal", 20.00, "C:\Users\PC\Desktop\icons\11.jpg");
itemPanel11.setBounds(379, 18, 140, 190);
drinksPanel.add(itemPanel11);
// Group 7: Item 1
ItemPanel itemPanel12 = new ItemPanel("D4", "PT Berry Bliss", 79.00, "C:\Users\PC\Desktop\icons\12.jpg");
itemPanel12.setBounds(552, 18, 140, 190);
drinksPanel.add(itemPanel12);
// Group 8: Item 1
ItemPanel itemPanel13 = new ItemPanel("D5", "CPT Bliss Shake", 79.00, "C:\Users\PC\Desktop\icons\13.jpg");
itemPanel13.setBounds(725, 18, 140, 190);
drinksPanel.add(itemPanel13);
// Group 9: Item 1
ItemPanel itemPanel14 = new ItemPanel("D6", "PT Caramel Drift", 69.00, "C:\Users\PC\Desktop\icons\14.jpg");
itemPanel14.setBounds(33, 226, 140, 190);
drinksPanel.add(itemPanel14);
// Group 10: Item 1
ItemPanel itemPanel15 = new ItemPanel("D7", "PTF Latte Chill", 89.00, "C:\Users\PC\Desktop\icons\15.jpg");
itemPanel15.setBounds(206, 226, 140, 190);
drinksPanel.add(itemPanel15);
// Desserts panel
dessertsPanel = createPanel(Color.green);
// Group 6: Item 1
ItemPanel itemPanel16 = new ItemPanel("C1", "PT Fudgy Delight", 59.00, "C:\Users\PC\Desktop\icons\16.jpg");
itemPanel16.setBounds(33, 18, 140, 190);
dessertsPanel.add(itemPanel16);
// Group 7: Item 1
ItemPanel itemPanel17 = new ItemPanel("C2", "PT Berry Muffin", 59.00, "C:\Users\PC\Desktop\icons\17.jpg");
itemPanel17.setBounds(206, 18, 140, 190);
dessertsPanel.add(itemPanel17);
add(drinksPanel);
add(dessertsPanel);
// Others panel
othersPanel = createPanel(Color.orange);
add(othersPanel);
JPanel panel2 = new JPanel(null); // Using null layout for panel2
panel2.setBounds(900, 0, 900, 930);
panel2.setBackground(Color.gray);
add(panel2);
Object[][] data = {
{10001, "Doe", 30, 69.00},
};
// Sample column names
Object[] columns = {"Item ID", "Item name", "Amount", "Price"};
// Create a default table model with sample data
DefaultTableModel model = new DefaultTableModel(data, columns);
// Create a JTable with the default table model
// Create a JTable with the default table model
JTable table1 = new JTable(model);
// Set the preferred width for the "Item name" column to be 20 pixels wider
int currentWidth = table1.getColumnModel().getColumn(1).getPreferredWidth();
table1.getColumnModel().getColumn(1).setPreferredWidth(currentWidth + 20);
JScrollPane scrollPane = new JScrollPane(table1);
scrollPane.setBounds(30, 20, 500, 600);
panel2.add(scrollPane);
resetbtn = new JButton("Reset");
resetbtn.setBounds(555, 20, 100, 40);
panel2.add(resetbtn);
printbtn = new JButton("Print");
printbtn.setBounds(555, 70, 100, 100);
panel2.add(printbtn);
JLabel lab1 = new JLabel("Total:");
lab1.setBounds(30, 630, 200, 50);
Font font = new Font("Arial", Font.BOLD, 50);
lab1.setFont(font);
panel2.add(lab1);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(null); // Using null layout for panel1
panel.setBounds(0, 50, 900, 880);
panel.setBackground(Color.white); // Set main panel color
ItemPanel itemPanel1 = new ItemPanel("M1", "PT Nuggets Meal", 99.00, "C:\Users\PC\Desktop\icons\1.jpg");
itemPanel1.setBounds(33, 18, 140, 190);
panel.add(itemPanel1);
// Group 2: Item 2
ItemPanel itemPanel2 = new ItemPanel("M2", "PT Chicken Meal", 127.00, "C:\Users\PC\Desktop\icons\2.jpg");
itemPanel2.setBounds(206, 18, 140, 190);
panel.add(itemPanel2);
// Group 3: Item 3
ItemPanel itemPanel3 = new ItemPanel("M3", "Creamy Spaghetti", 69.00, "C:\Users\PC\Desktop\icons\3.jpg");
itemPanel3.setBounds(379, 18, 140, 190);
panel.add(itemPanel3);
// Group 4: Item 4
ItemPanel itemPanel4 = new ItemPanel("M4", "Specially Carbonara", 69.00, "C:\Users\PC\Desktop\icons\4.jpg");
itemPanel4.setBounds(552, 18, 140, 190);
panel.add(itemPanel4);
// Group 5: Item 5
ItemPanel itemPanel5 = new ItemPanel("S1", "PT Nuggets Overload", 240.00, "C:\Users\PC\Desktop\icons\5.jpg");
itemPanel5.setBounds(725, 18, 140, 190);
panel.add(itemPanel5);
// Group 6: Item 1
ItemPanel itemPanel6 = new ItemPanel("S2", "PT Chicken Overload", 520.00, "C:\Users\PC\Desktop\icons\6.jpg");
itemPanel6.setBounds(33, 226, 140, 190);
panel.add(itemPanel6);
// Group 7: Item 1
ItemPanel itemPanel7 = new ItemPanel("S3", "BP Bliss", 210.00, "C:\Users\PC\Desktop\icons\7.jpg");
itemPanel7.setBounds(206, 226, 140, 190);
panel.add(itemPanel7);
// Group 8: Item 1
ItemPanel itemPanel8 = new ItemPanel("S4", "PT Fries", 49.00, "C:\Users\PC\Desktop\icons\8.jpg");
itemPanel8.setBounds(379, 226, 140, 190);
panel.add(itemPanel8);
// Group 9: Item 1
ItemPanel itemPanel9 = new ItemPanel("D1", "Coke", 20.00, "C:\Users\PC\Desktop\icons\9.jpg");
itemPanel9.setBounds(552, 226, 140, 190);
panel.add(itemPanel9);
// Group 10: Item 1
ItemPanel itemPanel10 = new ItemPanel("D2", "Sprite", 20.00, "C:\Users\PC\Desktop\icons\10.jpg");
itemPanel10.setBounds(725, 226, 140, 190);
panel.add(itemPanel10);
// Group 6: Item 1
ItemPanel itemPanel11 = new ItemPanel("D3", "Royal", 20.00, "C:\Users\PC\Desktop\icons\11.jpg");
itemPanel11.setBounds(33, 434, 140, 190);
panel.add(itemPanel11);
// Group 7: Item 1
ItemPanel itemPanel12 = new ItemPanel("D4", "PT Berry Bliss", 79.00, "C:\Users\PC\Desktop\icons\12.jpg");
itemPanel12.setBounds(206, 434, 140, 190);
panel.add(itemPanel12);
// Group 8: Item 1
ItemPanel itemPanel13 = new ItemPanel("D5", "CPT Bliss Shake", 79.00, "C:\Users\PC\Desktop\icons\13.jpg");
itemPanel13.setBounds(379, 434, 140, 190);
panel.add(itemPanel13);
// Group 9: Item 1
ItemPanel itemPanel14 = new ItemPanel("D6", "PT Caramel Drift", 69.00, "C:\Users\PC\Desktop\icons\14.jpg");
itemPanel14.setBounds(552, 434, 140, 190);
panel.add(itemPanel14);
// Group 10: Item 1
ItemPanel itemPanel15 = new ItemPanel("D7", "PTF Latte Chill", 89.00, "C:\Users\PC\Desktop\icons\15.jpg");
itemPanel15.setBounds(725, 434, 140, 190);
panel.add(itemPanel15);
// Group 6: Item 1
ItemPanel itemPanel16 = new ItemPanel("C1", "PT Fudgy Delight", 59.00, "C:\Users\PC\Desktop\icons\16.jpg");
itemPanel16.setBounds(33, 642, 140, 190);
panel.add(itemPanel16);
// Group 7: Item 1
ItemPanel itemPanel17 = new ItemPanel("C2", "PT Berry Muffin", 59.00, "C:\Users\PC\Desktop\icons\17.jpg");
itemPanel17.setBounds(206, 642, 140, 190);
panel.add(itemPanel17);
return panel;
}
private JPanel createPanel(Color color) {
JPanel panel = new JPanel(null); // Using null layout for panel1
panel.setBounds(0, 50, 900, 880);
panel.setBackground(color); // Set panel color
// Add components specific to the panel...
return panel;
}
class ItemPanel extends JPanel {
private String itemId;
private String itemName;
private double price;
private JButton iconButton;
private JSpinner spinner;
private JButton addButton;
public ItemPanel(String itemId, String itemName, double price, String imagePath) {
this.itemId = itemId;
this.itemName = itemName;
this.price = price;
setLayout(null); // Using null layout for custom positioning
setBackground(Color.white);
// Icon button
iconButton = new JButton();
ImageIcon icon = new ImageIcon(imagePath);
iconButton.setIcon(icon);
iconButton.setContentAreaFilled(false);
iconButton.setBorderPainted(false);
iconButton.setBounds(0, 0, 140, 140);
add(iconButton);
// Spinner
spinner = new JSpinner();
spinner.setBounds(0, 150, 70, 40);
add(spinner);
// Add button
addButton = new JButton("Add");
addButton.setBounds(80, 150, 60, 40);
add(addButton);
}
public String getItemId() {
return itemId;
}
public String getItemName() {
return itemName;
}
public double getPrice() {
return price;
}
}
private void switchToMainPanel() {
mainPanel.setVisible(true);
drinksPanel.setVisible(false);
dessertsPanel.setVisible(false);
othersPanel.setVisible(false);
}
private void switchToDrinksPanel() {
mainPanel.setVisible(false);
drinksPanel.setVisible(true);
dessertsPanel.setVisible(false);
othersPanel.setVisible(false);
}
private void switchToDessertsPanel() {
mainPanel.setVisible(false);
drinksPanel.setVisible(false);
dessertsPanel.setVisible(true);
othersPanel.setVisible(false);
}
private void switchToOthersPanel() {
mainPanel.setVisible(false);
drinksPanel.setVisible(false);
dessertsPanel.setVisible(false);
othersPanel.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Ordering order = new Ordering();
order.setVisible(true);
});
}
}
I tried making a model for the default table thingy for separate panels and it didnt work since I should have a single table for them compile with so ill be sticking with the model table as its what I initialized i think thats what I did and ya theres no error to that code but i really dont know how to start giving it functionality, I have debug the code by having it print item added and it does work but the table thing just doesnt
Gushiees is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.