简体   繁体   中英

JComboBox and ItemListener / ActionListener

I am creating a program for class where you have a JComboBox and when one option is selected it pops up a window with different options. I have one option that pops up a new window with two buttons on it.

First I'm not sure if I should be using ItemListener or ActionListener for the JComboBox options. Right now I have an ItemListener which I thought would work for just "The Matrix" option but it works for both options and I can't figure out why. I'll post all my code just in case but I'll add stars above and below the specified issue.

Thanks for any help or pointing me in the right direction!

public class MultiForm extends JFrame{


    private JComboBox menu;
    private JButton bluePill;
    private JButton redPill;
    private JLabel matrix;
    private int matrixSelection;
    private static String[] fileName = {"", "The Matrix", "Another Option"};

public MultiForm() {
    super("Multi Form Program");        
    setLayout(new FlowLayout());
    menu = new JComboBox(fileName);
    add(menu);

 *************************************************************************  
    TheHandler handler = new TheHandler();
    menu.addItemListener(handler);      
}

private class TheHandler implements ItemListener{
    public void itemStateChanged(ItemEvent event) {

        if(event.getStateChange() == ItemEvent.SELECTED) {
            menu.setSelectedItem("The Matrix");
            menu.getSelectedIndex();
*************************************************************************
            //Create a new window when "The Matrix" is clicked in the JCB
            JFrame newFrame = new JFrame();
            JPanel panel = new JPanel();

            newFrame.setLayout(new FlowLayout());
            newFrame.setSize(500, 300);
            newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
            add(panel, BorderLayout.CENTER);

            matrix = new JLabel("<html>After this, there is no turning back. "
                    + "<br>You take the blue pill—the story ends, you wake up "
                    + "<br>in your bed and believe whatever you want to believe."
                    + "<br>You take the red pill—you stay in Wonderland, and I show"
                    + "<br>you how deep the rabbit hole goes. Remember: all I'm "
                    + "<br>offering is the truth. Nothing more.</html>");
            newFrame.add(matrix, BorderLayout.NORTH);

            Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
            bluePill = new JButton("Blue Pill", bp);
            newFrame.add(panel.add(bluePill));  

            Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
            redPill = new JButton("Red Pill", rp);
            newFrame.add(panel.add(redPill));   


            newFrame.setVisible(true);
        }   
    }
}

public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400, 200);
    go.setVisible(true);
}
}

ItemListener and ActionListener will tell when something about the combo box has changed. You then need to ascertain WHAT has changed and take appropriate action

For example...

private class TheHandler implements ItemListener{
    public void itemStateChanged(ItemEvent event) {

        if(event.getStateChange() == ItemEvent.SELECTED) {
            Object source = event.getSource();
            if (source instanceof JComboBox) {
                JComboBox cb = (JComboBox)source;
                Object selectedItem = cb.getSelectedItem();
                if ("The Matrix".equals(selectedItem)) {
                    // Do the matrix
                } else if ("Another Option".equals(selectedItem)) {
                    // Do another option
                }
            }
        }   
    }
}

This simply checks what the selectedItem is and takes appropriate action according to what is selected. You could also use selectedIndex instead, which will return a int representing the selected item, but which ever is easier for you.

Have a look at How to Use Combo Boxes for more details

If all you want to know is when a item is selected, you might find a ActionListener simpler, as you don't need to check the state ( SELECTED / UNSELECTED ), as it only triggers on the selected state change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM