Search code examples
javaswingcombobox

Java combo box returned value to another frame


i am trying to make a quiz in java and i need to pass a returned value(index of a combo box) to another frame, but i don't know how to access the variable inside the previous frame from the main frame. I want to use the index for an if in the second frame(main one) something like this:

if(question==1 && domain==2){
      jlabel.setText(" ");
      //and so on
}

So, the domain is from the menu frame where i choose from a combo box and i want to use it's value in the main(second) frame where i do the if statement.


Solution

  • ((JComboBox)quizPanel.getComponent(i)).getSelectedIndex();
    

    'i' is the index of the JComboBox component. If you added the JComboBox to the JPanel first, then its index would be 0. You could then use its value in an if statement as in the following example:

    if (((JComboBox)quizPanel.getComponent(0)).getSelectedIndex() == 0) {
        System.out.println("Correct Answer");
    }
    else {
        System.out.println("Incorrect Answer");
    }