Question

I have 2 comboboxes and I need to make it so that when certain options are selected from the drop down list, certain results are outputted. How do I associate certain string variables or objects with multiple combobox selections. I'm not asking you to do my homework for me. Just need pointing in the right direction.

    public class gui extends JFrame implements ActionListener{

    String[] colour1 = {"red", "blue", "green", "orange", "brown","white", "black", "yellow", "purple", "pink"};
        String[] colour2 = {"red", "blue", "green", "orange", "brown","white", "black", "yellow", "purple", "pink"};

        JComboBox combo1 = new JComboBox(colour1);
        JComboBox combo2 = new JComboBox(colour2);

        JLabel message = new JLabel(); 
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new GridLayout(0, 1));

    public gui() {

         panel.add(combo1);
        panel.add(combo2);
        panel.add(message);
        frame.add(panel);
}

I'm using actionPerformed to catch the users input, and then output specific results. At the moment it only takes the value of one combobox and outputs a string. How do i make it take 2.

public void actionPerformed(ActionEvent e){
           if(e.getSource() == combo1){
               JCombobox cb = (JComboBox)e.getSource();
           String colours = (String) cb.getSelectedItem();

           switch(colours){
           case "red": message.setText("");
               break;
           case "blue": message.setText("");
               break;
           case "green": message.setText("");
               break;
           case "pink": message.setText("");
               break;
           case "purple":message.setText("");
               break;
           case "white": message.setText("");
               break;
           case "black": message.setText("");
               break;
           case "brown": message.setText("");
               break;
           case "orange": message.setText("");
               break;
           case "yellow": message.setText("");
               break;
           default: message.setText("");
           }
        }
    }
Was it helpful?

Solution

As it was pointed out in the comments, you probably don't need both arrays. When both combo boxes should contain the same values, then you can pass the same array to both combo boxes.

The actual question seems to be aiming at how to perform a specific action depending on the combination of the selections of two combo boxes.

I think there are two options for this: You could either store the combo boxes as instance variables, or you could store the selections of the combo boxes as instance variables. Since you're already storing the combo boxes as instance variables, this should be the easier one to go here. So you could do something like this:

@Override
public void actionPerformed(ActionEvent e)
{
    String color1 = (String)combo1.getSelectedItem();
    String color2 = (String)combo2.getSelectedItem();
    // Possibly check if either color is 'null' here

    if (color1.equals("blue") && color2.equals("yellow"))
    {
        message.setText("green");
    }
    ...
}

(Note: If you now intend to write a nested switch-statement like

switch(colour1)
{
    case "red": 
        switch(colour2)
        {
            // 10 cases...
        }
    break;

    // 10 x 10 cases...
}

you should think about a different approach, depending on what you want to do with these colors...)

OTHER TIPS

What you want is display a Value depending on the selected Values of the two comboboxes. Make combo1 and combo2 private fields. Write a private method to respond to the action event of both combo. Switch on combo values. Note that you can access both combo directly because they are class fields

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top