Search code examples
javaswingcombobox

Why does this only work on designed comboBox and not on hardcoded swing?


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


Solution

  • 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