As you can see below, I have created a JComboBox with elements in the 'options' array as the options in the list.
When a specific item in the list is selected, I want to display the JLabels 'one' or 'two'. E.g. selecting option one displays 'one' and selecting option two displays 'two' and removes 'one' from the display, and vice versa.
I know how to add the JLabels, with add(); but I don't know how to differentiate between which item is selected.
I have searched online and looked at the java docs but I couldn't find a relevant solution.
Thanks
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;
public class converterPage extends JFrame {
private JComboBox box;
private static String[] options = {"Option one", "Option two"};
private JLabel one, two;
public converterPage() {
super("Convert Units");
setLayout (null);
box = new JComboBox(options);
box.setBounds(10, 10, 150, 30);
add(box);
one = new JLabel("one");
two = new JLabel("two");
one.setBounds(170, 10, 150, 30);
two.setBounds(170, 10, 150, 30);
box.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
// depending on what is selected, i want to only display certain stuff
}
}
);
}
}