JComboBox Action listener

前端 未结 3 824
梦如初夏
梦如初夏 2020-12-07 04:54

I\'m having this problem. I have multiple JComboBoxes (5 total).

To each comboBox I add an ActionListener, but the same ActionListener for all of them, called:

相关标签:
3条回答
  • 2020-12-07 05:36

    @user650608 your questions isn't clear for me, do you mean - going this way, or am I wrong ?,

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener {
    
        private static final long serialVersionUID = 1L;
        private JComboBox mainComboBox;
        private JComboBox subComboBox;
        private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
    
        public ComboBoxTwo() {
            String[] items = {"Select Item", "Color", "Shape", "Fruit", "Size"};
            mainComboBox = new JComboBox(items);
            mainComboBox.addActionListener(this);
            mainComboBox.addItemListener(this);
            //prevent action events from being fired when the up/down arrow keys are used
            //mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
            getContentPane().add(mainComboBox, BorderLayout.WEST);
            subComboBox = new JComboBox();//  Create sub combo box with multiple models
            subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
            subComboBox.addItemListener(this);
            getContentPane().add(subComboBox, BorderLayout.CENTER);
            String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
            subItems.put(items[1], subItems1);
            String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
            subItems.put(items[2], subItems2);
            String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
            subItems.put(items[3], subItems3);
            String[] subItems4 = {"Select Size", "Big", "Middle", "Small"};
            subItems.put(items[4], subItems4);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            String item = (String) mainComboBox.getSelectedItem();
            Object o = subItems.get(item);
            if (o == null) {
                subComboBox.setModel(new DefaultComboBoxModel());
            } else {
                subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
            }
        }
    
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                if (e.getSource() == mainComboBox) {
                    if (mainComboBox.getSelectedIndex() != 0) {
                        FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
                                mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                    }
                } 
            }
        }
    
        private class FirstDialog extends JDialog {
    
            private static final long serialVersionUID = 1L;
    
            FirstDialog(final Frame parent, String winTitle, String msgString) {
                super(parent, winTitle);
                setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                JLabel myLabel = new JLabel(msgString);
                JButton bNext = new JButton("Stop Processes");
                add(myLabel, BorderLayout.CENTER);
                add(bNext, BorderLayout.SOUTH);
                bNext.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        setVisible(false);
                    }
                });
                javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                setLocationRelativeTo(parent);
                setSize(new Dimension(400, 100));
                setVisible(true);
            }
        }
    
        public static void main(String[] args) {
            JFrame frame = new ComboBoxTwo();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    0 讨论(0)
  • 2020-12-07 05:47

    Did you try using an ItemListener instead ?

    The doc says an ActionEvent is fired every time the combo box is edited.

    Regards, Stéphane

    0 讨论(0)
  • 2020-12-07 05:52

    Add separate action listeners instead of having one action listener run through if statements for each call. That section of the code will have logic that most likely has a bug that is causing the last combo box to be selected. (Maybe an else statement that should be else if, etc.).

    Separating it out will be more OO and will be more flexible long term.

    0 讨论(0)
提交回复
热议问题