0

I have created a user interface in Eclipse whereby the I require the user to select an option in the JComboBox and click the JButton to start the event. Depending on which option they select, a different class will be run and output its results. Everything is set up fine and the JButtons work on their own but I cannot get them to respond to changes in the JComboBox.

This is a sample of the code and the class which starts the interface (The full code is longer and contains more buttons etc. hence the extra columns and rows):

package projectFinal;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class test extends JFrame {

  public test() {

    setTitle("BIM Representative GUI");

    JPanel pane = new JPanel(new BorderLayout());

    int numberOfRows = 8;
    int numberOfColumns = 4;
    pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));

    JLabel metric1label = new JLabel(" Greenhouse Gas Emissions: ");
    pane.add(metric1label);
    JLabel metric1alabel = new JLabel("  ");
    pane.add(metric1alabel);
    String[] pollutants = { "Total","CO2", "CH4","N2O"};
    final JComboBox<String> cb1 = new JComboBox<String>(pollutants);
    cb1.setVisible(true);
    getContentPane().add(cb1);
    JButton button1 = new JButton("Check");
    pane.add(button1);

    getContentPane().add(pane);

    pack();

    button1.setToolTipText("This button will show the Greenhouse Gas Emissions");

    button1.addActionListener(new MyActionListener1());    

  }
  public class MyActionListener1 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String choice = (String)cb1.getSelectedItem();
            if(choice.equals("Total"))
        {
            GHGEmissions.UI();
        }
        if(choice.equals("CO2"))
        {
            CO2Emissions.UI();
        }
        if(choice.equals("CH4"))
         {
             CH4Emissions.UI();
         }
        if(choice.equals("N2O"))
             {
             N2OEmissions.UI();
        }
             }
}}

And the code for running the interface:

package projectFinal;

import projectFinal.test;

public class testRun {
      public static void main(String[] args) {
          test view = new test();
        view.setVisible(true);
      }
    } 

The JComboBox does not appear on the interface at all (it does when the String choice and if statements are removed). Does anybody know how I can fix this so that it will run the different classes depending on the JComboBox.

The only part that is showing a problem is cb1 in the line:

            String choice = (String)cb1.getSelectedItem();

Thanks

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

1 Answers1

1

cb1 is a local variable in you constructor, so you need to declare your ActionListener as annoynimus class in order to access cb1 variable,

button1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // you code.

        }
    });

Also tou need to compare String using .equals() method not ==.

So try to change your code to something like:

if(choice.equals("Total"))
{
    GHGEmissions.UI();
}

Refer to this for more details.

Community
  • 1
  • 1
Salah
  • 8,567
  • 3
  • 26
  • 43
  • Thanks but unfortunately it is still not working. I should have stated that the error is coming from the line: String choice = (String)cb1.getSelectedItem(); It is cb1 in this line which is highlighted. I will edit my question to include this. – user2916471 Apr 11 '14 at 20:13