Search code examples
javaswingjframeactionlistenerjcombobox

JComboBox: Wrong form appears after selection


I have some problems with a JComboBox with different items to select. When an item is selected I want a new, related form to appear where the user can enter more information for the selected item (only one form per selection). In my program, the wrong form appears, or several forms appear in the wrong order. For example, if I run the program and select "Smycke" (Jewelry, parts of my code are in Swedish), nothing happens until I selected another option, for instance "Aktie" (Stock) - then the "Smycke" form appears followed by the "Aktie" form. How can I make the correct form appear?

Parts of my code:

public class Program extends JFrame {

    private ArrayList<Valueables> allValueables = new ArrayList<>();

    private JTextArea display;
    private JRadioButton sortName;
    private JRadioButton sortValue;

    private String[] valueables = { "Smycke", "Aktie", "Apparat" };
    private JComboBox<String> box = new JComboBox<String>(valueables);


    Program() {
        super("Sakregister");
        JPanel top = new JPanel();
        top.add(new JLabel("Värdesaker"));
        add(top, BorderLayout.NORTH);

        JPanel left = new JPanel();
        add(left, BorderLayout.WEST);

        JPanel right = new JPanel();
        right.add(new JLabel("Sortering"));
        sortName = new JRadioButton("Namn", true);
        right.add(sortName);
        sortValue = new JRadioButton("Värde");
        right.add(sortValue);
        ButtonGroup groupb = new ButtonGroup();
        groupb.add(sortName);
        groupb.add(sortValue);
        add(right, BorderLayout.EAST);
        right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));

        JPanel bottom = new JPanel();
        bottom.add(new JLabel("Nytt:"));

        bottom.add(box);
        box.addActionListener(new BoxLis());
        add(bottom, BorderLayout.SOUTH);

        display = new JTextArea();
        JScrollPane scroll = new JScrollPane(display);
        display.setEditable(false);
        display.setWrapStyleWord(true);
        add(scroll, BorderLayout.CENTER);

        setSize(600, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    class BoxLis implements ActionListener {
        public void actionPerformed(ActionEvent ave) {

            if (box.getSelectedIndex() == 0) {
                box.addActionListener(new JewelryLis());
            } else if (box.getSelectedIndex() == 1) {
                box.addActionListener(new StockLis());
            } else if (box.getSelectedIndex() == 2) {
                box.addActionListener(new DeviceLis());
            }
        }
    }

    class JewelryLis implements ActionListener {
        public void actionPerformed(ActionEvent ave) {

            JewelryForm f = new JewelryForm();
            try {
                int svar = JOptionPane.showConfirmDialog(Program.this, f, "Nytt smycke", JOptionPane.OK_CANCEL_OPTION);
                if (svar != JOptionPane.OK_OPTION)
                    return;

                String name = f.getName();
                int stones = f.getStones();
                boolean isGold = f.getGold();

                addJewelry(name, stones, isGold);

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(Program.this, "Fel inmatning!");
            }
        }
    }

    class StockLis implements ActionListener {
        public void actionPerformed(ActionEvent ave) {

            StockForm f = new StockForm();
            try {
                int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny aktie", JOptionPane.OK_CANCEL_OPTION);
                if (svar != JOptionPane.OK_OPTION)
                    return;

                String name = f.getName();
                int amount = f.getAmount();
                int stockPrice = f.getStockPrice();

                addStock(name, amount, stockPrice);

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(Program.this, "Fel inmatning!");
            }
        }

    }

    class DeviceLis implements ActionListener {
        public void actionPerformed(ActionEvent ave) {

            DeviceForm f = new DeviceForm();
            try {
                int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny apparat", JOptionPane.OK_CANCEL_OPTION);
                if (svar != JOptionPane.OK_OPTION)
                    return;

                String name = f.getName();
                int purchasePrice = f.getPurchasePrice();
                int wear = f.getWear();

                addDevice(name, purchasePrice, wear);

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(Program.this, "Fel!");
            }
        }

    }

Solution

  • In your BoxLis listner instead of showing appropriate form you are adding actionlistnere to your combo box.

    Following changes to your code will fix the issue.

    box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (box.getSelectedIndex() == 0) {
                    showJewlryForm();
                } else if (box.getSelectedIndex() == 1) {
                    showStockForm();
                } else if (box.getSelectedIndex() == 2) {
                    showDeviceForm();
                }
    
            }
        });    
    
        private void showJewlryForm() {
            JewelryForm f = new JewelryForm();
            try {
                int svar = JOptionPane.showConfirmDialog(Program.this, f, "Nytt smycke", JOptionPane.OK_CANCEL_OPTION);
                if (svar != JOptionPane.OK_OPTION)
                    return;
    
                String name = f.getName();
                int stones = f.getStones();
                boolean isGold = f.getGold();
    
                addJewelry(name, stones, isGold);
    
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(Program.this, "Fel inmatning!");
            }
        }
    
        private void showStockForm() {
    
            StockForm f = new StockForm();
            try {
                int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny aktie", JOptionPane.OK_CANCEL_OPTION);
                if (svar != JOptionPane.OK_OPTION)
                    return;
    
                String name = f.getName();
                int amount = f.getAmount();
                int stockPrice = f.getStockPrice();
    
                addStock(name, amount, stockPrice);
    
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(Program.this, "Fel inmatning!");
            }
    
        }
    
        private void showDeviceForm() {
    
            DeviceForm f = new DeviceForm();
            try {
                int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny apparat", JOptionPane.OK_CANCEL_OPTION);
                if (svar != JOptionPane.OK_OPTION)
                    return;
    
                String name = f.getName();
                int purchasePrice = f.getPurchasePrice();
                int wear = f.getWear();
    
                addDevice(name, purchasePrice, wear);
    
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(Program.this, "Fel!");
            }
    
        }