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