Having recently found and implemented JComboBox in my code, I cant seem to find anywhere that will help me with the action or itemlistener. What I want to do is set certain TextFields to "uneditable" if "NO" in my drop down menu in my ComboBox is selected...and editable if "YES" in the ComboBox is selected. I dont know if to use itemlisteneres or actionlisteners. Ive tried both and failed miserablly both times. Can someone help?
public class ComboDemo extends JPanel implements ActionListener{
public ComboDemo(){
String[] yesNo= {"YES","NO!"};
JComboBox comboBox = new JComboBox(yesNo);
comboBox.addActionListener(this);
add(comboBox);
}
public void actionPerformed(ActionEvent evt){
String selected;
JComboBox c = (JComboBox)evt.getSource();
selected = (String)c.getSelectedItem();
System.out.println("You selected: "+selected);
}
public static void createAndShowGUI(){
JFrame myFrame = new JFrame();
JPanel panel = new ComboDemo();
myFrame.add(panel);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(200, 100);
myFrame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Then you will change your components in the actionPerformed function based on the selected item.
eg. myComponent.setEditable(false);