0

Newbie question:

So this code here

public void comboItemItemStateChanged(java.awt.event.ItemEvent evt) {  
        ArrayList<String> arrayItem = new ArrayList<>();
        Iterator<String> iter;
        if(comboGroup.getSelectedItem().equals("Betta Fish")){
            comboItem.removeAllItems();
            arrayItem.add("Plakat");
            arrayItem.add("Halfmoon");
            arrayItem.add("Crown Tail");
            arrayItem.add("Double Tail");
            iter = arrayItem.iterator();
            while(iter.hasNext()){
                comboItem.addItem(iter.next());
            }
        }
        else if(comboGroup.getSelectedItem().equals("Snails")){
            comboItem.removeAllItems();
            arrayItem.add("Apple Horn");
            arrayItem.add("RamsHorn");
            arrayItem.add("Pond Snail");
            arrayItem.add("Assassin Snail");
            iter = arrayItem.iterator();
            while(iter.hasNext()){
                comboItem.addItem(iter.next());
            }

works when I try it on comboBoxes from Design tab in NetBeans. But when I try to apply it to my coded ComboBox, I get a message from evt saying Unused method parameters should be removed. Can I get an explanation why and what is the alternative for hardcoded comboBox?

Purpose of code: a dynamic comboBox so whatever I pick from comboBox1 will have each own set of lists for comboBox2

NOTE: I also tried to change comboItemItemStateChanged to just itemStatChanged.

Source code of my project: https://github.com/kontext66/GuwiPos/blob/main/GuwiPos

Sorry for the confusion everyone, I do have a main class.

public class Main{
    
    public static void main(String[] args){
        
        new GuwiPos();
        new HelpWin();
        
    }
    
}

Main.java, GuwiPos.java

beepmorp
  • 5
  • 8
  • 1
    There are a few things to unpack here. I will draft an answer, but it will take a bit of time. In the mean time, let me ask you one thing: who calls `comboItemItemStateChanged` method? – hfontanez Feb 01 '22 at 04:46
  • Also, the code on the link doesn't even have a `main` method. How do you run this code? – hfontanez Feb 01 '22 at 04:51
  • 1
    Please post [mre] – c0der Feb 01 '22 at 05:39
  • 1
    *so whatever I pick from comboBox1 will have each own set of lists for comboBox2* - Why would you add all the items to an ArrayList and then iterate through the ArrayList to populate the combo box. The ArrayList is not needed. Just add the items directly to the combo box. Or see: https://stackoverflow.com/questions/4982260/binding-comboboxes-in-swing/4982576#4982576 for a working example. – camickr Feb 01 '22 at 14:59

2 Answers2

2

It's quite simple, really.
The message is just NetBeans informing you that the code of method comboItemItemStateChanged does not reference the method parameter evt. It is not an error nor even a warning. You can ignore it. NetBeans displays these "hints" in its editor when you write code whereas NetBeans GUI builder generates code.

Note that method comboItemItemStateChanged will be called each time an item in the JComboBox is selected and also each time an item is de-selected. I'm guessing that you only want the code to run when an item is selected, hence the first line of method comboItemItemStateChanged should be

if (evt.getStateChange() == ItemEvent.SELECTED) {

and then you are referencing the method parameter and the "hint" will go away.

Refer to How to Write an Item Listener

Edit

I think maybe you would benefit from learning about GUI design. I have never seen one like yours. I would like to know how you arrived at that design. In any case, the below code addresses only the functionality whereby the list in comboItem adjusts depending on the selected item in comboGroup.

In my experience, removing and adding items to the JComboBox via methods removeAllItems and addItem, is very slow. It is much more efficient to simply replace the JComboBox model which is what I have done in the below code. For small lists such as yours, the difference will not be noticed but it increases as the lists get larger.

Also, the [JComboBox] selected item is in the evt parameter to method comboItemItemStateChanged.

Your original code did not add an item listener to comboGroup nor did it have a main method. I have added these in the below code. Note that the item listener is added via a method reference.

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author kahbg
 */
public class GuwiPos extends JFrame implements ActionListener {
    public static final Map<String, ComboBoxModel<String>>  LISTS;

    JButton buttonBasket;
    JButton buttonHelp;
    JTextField txtGroup;
    JTextField txtItem;
    JTextField txtQty;
    JTextField txtTotal;
    JTextField txtPrice;
    JComboBox<String> comboGroup;
    JComboBox<String> comboItem;

    static {
        LISTS = new HashMap<>();
        Vector<String> v = new Vector<>();
        v.add("Plakat");
        v.add("Halfmoon");
        v.add("Crown Tail");
        v.add("Double Tail");
        ComboBoxModel<String> model = new DefaultComboBoxModel<>(v);
        LISTS.put("Betta Fish", model);
        v = new Vector<>();
        v.add("Apple Horn");
        v.add("RamsHorn");
        v.add("Pond Snail");
        v.add("Assassin Snail");
        model = new DefaultComboBoxModel<>(v);
        LISTS.put("Snails", model);
        v = new Vector<>();
        v.add("Small Fine Net");
        v.add("Large Fine Net");
        v.add("Flaring Mirror");
        v.add("Aquarium Hose");
        model = new DefaultComboBoxModel<>(v);
        LISTS.put("Supplies", model);
        v = new Vector<>();
        v.add("Tubifex");
        v.add("Daphnia");
        v.add("Optimum Pellets");
        model = new DefaultComboBoxModel<>(v);
        LISTS.put("Food", model);
    }

    GuwiPos() {

        // ComboBox
        String[] products = {"Select Item...", "Betta Fish", "Snails", "Supplies", "Food"};
        comboGroup = new JComboBox<String>(products);
        comboGroup.addItemListener(this::comboItemItemStateChanged);
        comboGroup.setBounds(130, 35, 150, 40);

        comboItem = new JComboBox<String>();
        comboItem.setBounds(130, 85, 150, 40);

        // TextFields
        txtPrice = new JTextField();
        txtPrice.setBounds(130, 135, 150, 40);

        txtQty = new JTextField();
        txtQty.setBounds(130, 185, 150, 40);

        txtTotal = new JTextField();
        txtTotal.setBounds(130, 235, 150, 40);

        // txtTotalAmt = new JTextField(); to code later on

        // txtPaid = new JTextField(); to code later on

        // txtChange = new JTextField(); to code later on

        // Labels
        // Product
        JLabel labelProduct = new JLabel();
        labelProduct.setText("Item Group");
        labelProduct.setBounds(10, 5, 100, 100);
        // Item
        JLabel labelItem = new JLabel();
        labelItem.setText("Item");
        labelItem.setBounds(10, 55, 100, 100);
        // Price
        JLabel labelPrice = new JLabel();
        labelPrice.setText("Price");
        labelPrice.setBounds(10, 105, 100, 100);
        // Qty
        JLabel labelQty = new JLabel();
        labelQty.setText("Quantity");
        labelQty.setBounds(10, 155, 100, 100);
        // Total
        JLabel labelTotal = new JLabel();
        labelTotal.setText("Total");
        labelTotal.setBounds(10, 205, 100, 100);

        // Buttons
        buttonBasket = new JButton();
        buttonBasket.setBounds(0, 0, 300, 50);
        buttonBasket.setText("Add to Basket");
        buttonBasket.setFocusable(false);
        buttonBasket.setBorder(BorderFactory.createEtchedBorder());
        buttonBasket.addActionListener(this);

        JButton buttonPay = new JButton();
        buttonPay.setText("PAY");
        buttonPay.setBounds(0, 50, 150, 100);
        buttonPay.setFocusable(false);
        buttonPay.setBorder(BorderFactory.createEtchedBorder());
        buttonPay.addActionListener(e -> System.out.println("Payment Success"));

        JButton buttonCancel = new JButton();
        buttonCancel.setText("CANCEL");
        buttonCancel.setBounds(0, 150, 150, 100);
        buttonCancel.setFocusable(false);
        buttonCancel.setBorder(BorderFactory.createEtchedBorder());
        buttonCancel.addActionListener(e -> System.out.println("Transaction Cancelled"));

        JButton buttonDiscount = new JButton();
        buttonDiscount.setText("Apply Discount?");
        buttonDiscount.setBounds(150, 50, 150, 50);
        buttonDiscount.setFocusable(false);
        buttonDiscount.setBorder(BorderFactory.createEtchedBorder());
        buttonDiscount.addActionListener(e -> System.out.println("20% Discount Applied"));

        JButton buttonHelp = new JButton();
        buttonHelp.setText("HELP");
        buttonHelp.setBounds(150, 100, 150, 100);
        buttonHelp.setFocusable(false);
        buttonHelp.setBorder(BorderFactory.createEtchedBorder());

        JButton buttonDelete = new JButton();
        buttonDelete.setText("DELETE");
        buttonDelete.setBounds(150, 200, 150, 50);
        buttonDelete.setFocusable(false);
        buttonDelete.setBorder(BorderFactory.createEtchedBorder());
        buttonDelete.addActionListener(e -> System.out.println("Item Deleted"));

        // Panels
        // Left contains item and price
        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        bluePanel.setBounds(0, 0, 300, 300); // x,y,width,height
        bluePanel.setLayout(null);
        // Right contains product basket
        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.red);
        redPanel.setBounds(300, 0, 600, 300); // x,y,width,length
        redPanel.setLayout(null);
        // Bottom Left contains buttons pay,change,discount
        JPanel greenPanel = new JPanel();
        greenPanel.setBackground(Color.green);
        greenPanel.setBounds(0, 300, 300, 450);// x,y,width,length
        greenPanel.setLayout(null);
        // Bottom Right contains total amount
        JPanel yellowPanel = new JPanel();
        yellowPanel.setBackground(Color.yellow);
        yellowPanel.setBounds(0, 300, 900, 450);// x,y,width,length
        yellowPanel.setLayout(null);

        this.setTitle("Open Betta POS");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setSize(900, 590);
        this.setResizable(false);
        this.setVisible(true);

        // ADDED AWT
        bluePanel.add(txtQty);
        bluePanel.add(txtTotal);
        bluePanel.add(txtPrice);
        bluePanel.add(comboItem);
        bluePanel.add(comboGroup);
        bluePanel.add(labelProduct);
        bluePanel.add(labelItem);
        bluePanel.add(labelPrice);
        bluePanel.add(labelQty);
        bluePanel.add(labelTotal);
        greenPanel.add(buttonBasket);
        greenPanel.add(buttonPay);
        greenPanel.add(buttonCancel);
        greenPanel.add(buttonDiscount);
        greenPanel.add(buttonHelp);
        greenPanel.add(buttonDelete);
        this.add(bluePanel);
        this.add(redPanel);
        this.add(greenPanel);
        this.add(yellowPanel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonBasket) {
            System.out.println("Added Item to Basket: " + comboGroup.getSelectedItem() + "\n"
                    + comboItem.getSelectedItem());
        }
    }

    // This is not working
    public void comboItemItemStateChanged(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            Object selection = evt.getItem();
            ArrayList<String> arrayItem = new ArrayList<>();
            Iterator<String> iter;
            if (selection.equals("Betta Fish")) {
                comboItem.setModel(LISTS.get("Betta Fish"));
            }
            else if (selection.equals("Snails")) {
                comboItem.setModel(LISTS.get("Snails"));
            }
            else if (selection.equals("Supplies")) {
                comboItem.setModel(LISTS.get("Supplies"));
            }
            else if (selection.equals("Food")) {
                comboItem.setModel(LISTS.get("Food"));
            }
            else if (selection.equals("Select Item...")) {
                comboItem.removeAllItems();
                arrayItem.add("Select Item...");
                iter = arrayItem.iterator();
                while (iter.hasNext()) {
                    comboItem.addItem(iter.next());
                }
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new GuwiPos());
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • Thank you for the information you've given me, I've learned a lot from it. As for the approach on my GUI, I'm new to coding java, and I've been following a Youtube playlist for lectures, soo everything I've learned so far from it, I applied to the min project. That's why everything is manually put in and it's so messy. I appreciate your version of code but I haven't gotten into `Vector` and `HashMap` yet so I can't really understand it that much yet. – beepmorp Feb 02 '22 at 01:20
1

Your code (improved version - minus one ActionListener)

public class SwingApp {
    
    private static JComboBox<String> comboItem;
    private static JComboBox<String> productCombo;
    
    public static void main (String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            
            @Override
            public void run () {
                createAndShowGUI();
            }
        });
    }
    
    private static void createAndShowGUI () {
        
        JFrame frame = createMainFrame();
        
        JPanel bluePanel = createBluePanel();
        JPanel greenPanel = createGreenPanel();
        JPanel redPanel = createRedPanel();
        JPanel yellowPanel = createYellowPanel();
        
        frame.add(bluePanel);
        frame.add(greenPanel);
        frame.add(redPanel);
        frame.add(yellowPanel);
        frame.setVisible(true);
    }
    
    private static JFrame createMainFrame () {
        JFrame frame = new JFrame("Open Betta POS");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(900, 590);
        frame.setResizable(false);
        return frame;
    }
    
    private static JPanel createBluePanel () {
        ComboSelectionListener productComboListener = new ComboSelectionListener();
        JPanel panel = new JPanel(null); // Sets layout manager to null which is a bad idea!
        String[] products =
            {"Select Item...", "Betta Fish", "Snails", "Supplies", "Food"};
        productCombo = new JComboBox<>(products);
        productCombo.setBounds(130, 35, 150, 40);
        productCombo.setActionCommand("selectProduct");
        productCombo.addActionListener(productComboListener);
        comboItem = new JComboBox<>();
        comboItem.setBounds(130, 85, 150, 40);
        
        // TextFields
        JTextField txtPrice = new JTextField();
        txtPrice.setBounds(130, 135, 150, 40);
        
        JTextField txtQty = new JTextField();
        txtQty.setBounds(130, 185, 150, 40);
        
        JTextField txtTotal = new JTextField();
        txtTotal.setBounds(130, 235, 150, 40);
        JLabel labelProduct = new JLabel();
        labelProduct.setText("Item Group");
        labelProduct.setBounds(10, 5, 100, 100);
        // Item
        JLabel labelItem = new JLabel();
        labelItem.setText("Item");
        labelItem.setBounds(10, 55, 100, 100);
        // Price
        JLabel labelPrice = new JLabel();
        labelPrice.setText("Price");
        labelPrice.setBounds(10, 105, 100, 100);
        // Qty
        JLabel labelQty = new JLabel();
        labelQty.setText("Quantity");
        labelQty.setBounds(10, 155, 100, 100);
        // Total
        JLabel labelTotal = new JLabel();
        labelTotal.setText("Total");
        labelTotal.setBounds(10, 205, 100, 100);
        
        panel.setBackground(Color.blue);
        panel.setBounds(0, 0, 300, 300); // x,y,width,height
        panel.add(txtQty);
        panel.add(txtTotal);
        panel.add(txtPrice);
        panel.add(comboItem);
        panel.add(productCombo);
        panel.add(labelProduct);
        panel.add(labelItem);
        panel.add(labelPrice);
        panel.add(labelQty);
        panel.add(labelTotal);
        return panel;
        
    }
    
    private static JPanel createGreenPanel () {
        JPanel panel = new JPanel(null);
        panel.setBackground(Color.green);
        panel.setBounds(0, 300, 300, 450);// x,y,width,length
        JButton buttonBasket = new JButton();
        buttonBasket.setBounds(0, 0, 300, 50);
        buttonBasket.setText("Add to Basket");
        buttonBasket.setFocusable(false);
        buttonBasket.setBorder(BorderFactory.createEtchedBorder());
        buttonBasket
            .addActionListener(e -> System.out.println("Added Item to Basket: "
                + productCombo.getSelectedItem() + "\n" + comboItem.getSelectedItem()));
        
        JButton buttonPay = new JButton();
        buttonPay.setText("PAY");
        buttonPay.setBounds(0, 50, 150, 100);
        buttonPay.setFocusable(false);
        buttonPay.setBorder(BorderFactory.createEtchedBorder());
        buttonPay.addActionListener(e -> System.out.println("Payment Success"));
        
        JButton buttonCancel = new JButton();
        buttonCancel.setText("CANCEL");
        buttonCancel.setBounds(0, 150, 150, 100);
        buttonCancel.setFocusable(false);
        buttonCancel.setBorder(BorderFactory.createEtchedBorder());
        buttonCancel
            .addActionListener(e -> System.out.println("Transaction Cancelled"));
        
        JButton buttonDiscount = new JButton();
        buttonDiscount.setText("Apply Discount?");
        buttonDiscount.setBounds(150, 50, 150, 50);
        buttonDiscount.setFocusable(false);
        buttonDiscount.setBorder(BorderFactory.createEtchedBorder());
        buttonDiscount
            .addActionListener(e -> System.out.println("20% Discount Applied"));
        
        JButton buttonHelp = new JButton();
        buttonHelp.setText("HELP");
        buttonHelp.setBounds(150, 100, 150, 100);
        buttonHelp.setFocusable(false);
        buttonHelp.setBorder(BorderFactory.createEtchedBorder());
        
        JButton buttonDelete = new JButton();
        buttonDelete.setText("DELETE");
        buttonDelete.setBounds(150, 200, 150, 50);
        buttonDelete.setFocusable(false);
        buttonDelete.setBorder(BorderFactory.createEtchedBorder());
        buttonDelete.addActionListener(e -> System.out.println("Item Deleted"));
        
        panel.add(buttonBasket);
        panel.add(buttonPay);
        panel.add(buttonCancel);
        panel.add(buttonDiscount);
        panel.add(buttonHelp);
        panel.add(buttonDelete);
        
        return panel;
    }
    
    private static JPanel createRedPanel () {
        JPanel panel = new JPanel(null);
        panel.setBackground(Color.red);
        panel.setBounds(300, 0, 600, 300); // x,y,width,length
        return panel;
    }
    
    private static JPanel createYellowPanel () {
        JPanel panel = new JPanel();
        panel.setBackground(Color.yellow);
        panel.setBounds(0, 300, 900, 450); // x,y,width,length
        return panel;
    }
    
    private static class ComboSelectionListener implements ActionListener {
        
        @Override
        public void actionPerformed (ActionEvent e) {
            JComboBox<String> comboGroup = (JComboBox<String>) e.getSource();
            
            ArrayList<String> arrayItem = new ArrayList<>();
            Iterator<String> iter;
            if (comboGroup.getSelectedItem().equals("Betta Fish")) {
                comboItem.removeAllItems();
                arrayItem.add("Plakat");
                arrayItem.add("Halfmoon");
                arrayItem.add("Crown Tail");
                arrayItem.add("Double Tail");
                iter = arrayItem.iterator();
                while (iter.hasNext()) {
                    comboItem.addItem(iter.next());
                }
            } else if (comboGroup.getSelectedItem().equals("Snails")) {
                comboItem.removeAllItems();
                arrayItem.add("Apple Horn");
                arrayItem.add("RamsHorn");
                arrayItem.add("Pond Snail");
                arrayItem.add("Assassin Snail");
                iter = arrayItem.iterator();
                while (iter.hasNext()) {
                    comboItem.addItem(iter.next());
                }
            } else if (comboGroup.getSelectedItem().equals("Supplies")) {
                comboItem.removeAllItems();
                arrayItem.add("Small Fine Net");
                arrayItem.add("Large Fine Net");
                arrayItem.add("Flaring Mirror");
                arrayItem.add("Aquarium Hose");
                iter = arrayItem.iterator();
                while (iter.hasNext()) {
                    comboItem.addItem(iter.next());
                }
            } else if (comboGroup.getSelectedItem().equals("Food")) {
                comboItem.removeAllItems();
                arrayItem.add("Tubifex");
                arrayItem.add("Daphnia");
                arrayItem.add("Optimum Pellets");
                iter = arrayItem.iterator();
                while (iter.hasNext()) {
                    comboItem.addItem(iter.next());
                }
            } else if (comboGroup.getSelectedItem().equals("Select Item...")) {
                comboItem.removeAllItems();
                arrayItem.add("Select Item...");
                iter = arrayItem.iterator();
                while (iter.hasNext()) {
                    comboItem.addItem(iter.next());
                }
            }
        }
    }
}

Improvements made (aside from fixing the main issue)

  1. Created a Swing (main) class that is not a Swing component
  2. Main class doesn't implement ActionListener
  3. Used SwingUtilities to launch Swing Application
  4. Created methods to encapsulate the details involving the creation of components
  5. Minimized the scope of variables

enter image description here

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • Why are all the methods and inner classes static? – Gilbert Le Blanc Feb 01 '22 at 06:33
  • @GilbertLeBlanc because I was being lazy. That's also the reason why I have one class and inner class for the listener. I could've also used an `ItemListener` instead of an `ActionListener` but it was easier to adapt the OP's nested `if` to handle populating the item combo based on the product combo's item selection. – hfontanez Feb 01 '22 at 06:36
  • Hi, thank you for your version. I can read and understand that this is closer to my code than the other one, but both versions are great. I'll be looking into `SwingUtilities`. But may I ask, if there will be any major changes considering that I did have a `main` method? I forgot to post it on my first post but it's up on the edited version now. – beepmorp Feb 02 '22 at 01:25
  • I tried to run your code on my IDE sir but I get an error on this part ```buttonBasket .addActionListener(e -> System.out.println("Added Item to Basket: " + productCombo.getSelectedItem() + "\n" + comboItem.getSelectedItem()));``` Saying that it cannot find the symbol of `productCombo` – beepmorp Feb 02 '22 at 02:23
  • @beepmorp Swing applications MUST be launched as shown here. That's a major change that affects performance (see https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). There is also the fact that your entire application is in a single class. That is not desirable. I did that here because of constraints, but you should consider refactoring this code into multiple classes. – hfontanez Feb 02 '22 at 02:24
  • I posted the updated version. – hfontanez Feb 02 '22 at 02:25
  • @beepmorp *both versions are great* - actually you were given 3 versions. Neither of the two answers here are great because they both use nested if/else statements in the ActionListener which indicates poor design. The usage of the ArrayList in the listener is also unnecessary as was stated in the original comments. – camickr Feb 02 '22 at 05:06
  • @camickr I simply used the same nested if/else the OP in order for me to provide a quick answer. If you read my reply to Gilbert LeBlanc you will see that. – hfontanez Feb 02 '22 at 05:38
  • @camickr with all due respect, I know what I wrote. In case you missed it, here's a quote "_I could've also used an ItemListener instead of an ActionListener but it was easier to adapt the OP's nested if to handle populating the item combo based on the product combo's item selection_". If you look at that piece of code, it was a direct copy and paste from the OPs code. As I stated, I did it because of "laziness". Actually, it was late here and I just wanted to finish the post quickly so I could go to bed. – hfontanez Feb 02 '22 at 15:29
  • Yes, I missed that. If you know the solution is not the best then that should have been addressed with the original answer not as a comment. The OP should be aware of the limitations of the answer. My comment which was addressed to the OP (not you, since I know you just copied the original code) was to get him to revisit the code and restructure the code. – camickr Feb 02 '22 at 17:21
  • @camickr You are right about that. I should've pointed that out. I normally do that; just forgot this time. I started listing the improvements I made to the original code and totally neglected to point out that the code in the action listener was not the best (to say the least). I was just too tired. – hfontanez Feb 02 '22 at 17:41
  • I highly appreciate all the knowledge you've shared with me here. I didn't notice the comment on the main post at first, sorry. But since I'm still new to this, the version that @hfontanez created was more relatable to me since my knowledge of java is only up to here for now. But I will try and study the link you've sent me @camickr. For future reference, I am planning to add prices for the items that will be dependent on `comboBox1` should I proceed with the `binded comboBox swing` approach starting now, or may I proceed with the `ArrayList` and study the former later on? – beepmorp Feb 03 '22 at 01:33
  • @hfontanez I apologize for this question but when you say you recommend refactoring my code into multiple classes, should I create separate class files for each panels, or just new lines for access modifiers + class name in a single class file? – beepmorp Feb 03 '22 at 01:41
  • @beepmorp potentially separate classes for each panel since each panel has a specific job or function to perform. "God" classes are never a good thing outside Academia. Just like you create different applications (and not an universal application that does everything), you should create classes and methods that do a very specific job. That said, if you have components that do similar jobs, then you should create a generalized class that change (polymorphism) into one or the other depending on the parameters injected into the class when constructed. – hfontanez Feb 03 '22 at 01:48
  • @beepmorp for example, Java provides a `JPanel` class that you could reuse to create all kinds of panels. But, you won't use it to create a text field; even though you could technically do that. For text fields, you would use `JTextField`. Both of those classes 1) have a distinct function, and 2) are abstracted enough to where you could create different types of panel and text fields respectively. – hfontanez Feb 03 '22 at 01:51
  • I understand, I will try and implement that. Just a quick update, I understood @camickr's `Hashtable` method now and that's how I approached the `comboBox` issue. My next step would be assigning a price for each item. I highly appreciate all of your help and walkthroughs. Thanks a lot. – beepmorp Feb 03 '22 at 02:52