GUI ComboBox Opens Other GUIs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MiziaQ
    New Member
    • Nov 2007
    • 63

    GUI ComboBox Opens Other GUIs

    Hey, I have a problem which I need to resolve for my project. I want to open a different GUI class based on the selection made from a combobox:
    Code:
     public void actionPerformed(ActionEvent event) { 
             JComboBox comboBox = (JComboBox)event.getSource(); 
             String selection = (String)comboBox.getSelectedItem(); 
             if (selection == "Price Elasticity of Demand") { 
                runGUI 
             }
    How can I do this in java ?
    Furthermore, how can i call a method from a different class when a button is clicked ?

    Cheers in advance ;)
    Last edited by Nepomuk; Nov 25 '08, 11:19 PM. Reason: Please use [CODE] tags!
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by MiziaQ
    Code:
     public void actionPerformed(ActionEvent event) { 
             JComboBox comboBox = (JComboBox)event.getSource(); 
             String selection = (String)comboBox.getSelectedItem(); 
             if (selection == "Price Elasticity of Demand") { 
                runGUI 
             }
    First of all, you don't compare Strings like that in Java. Try
    Code:
    selection.equals("Price Elasticity of Demand")
    instead.

    Then, your idea seems to be good: use if's and elses. So for example:
    Code:
    String selection = (String)comboBox.getSelectedItem(); 
    if (selection.equals("First option")) { 
       runGUI1();
    } else if (selection.equals("Second option")) { 
      runGUI2();
    } else if (selection.equals("Third option")) { 
      runGUI3();
    } // etc.
    Greetings,
    Nepomuk

    Comment

    Working...