Hi,
I have a question about setSelectedItem method of JComboBox.
actionPerformed method of my class is called by Java, when I call setSelectedItem method of JComboBox.
1. Is there a way to stop calling the actionPeformed method when I execute setSelectedItem method of JComboBox.
My second issue is when I call setSelectedItem method within actionPerformed method, actionPerfromed method is not called again.
2. Why doesn't Java call actionPeformed method again?
package giu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class TestEvents extends JFrame implements ActionListener{
public JComboBox comboBox = new JComboBox();
public TestEvents() {
comboBox.addItem("Item 1");
comboBox.addItem("Item 2");
comboBox.addItem("Item 3");
comboBox.addActionListener(this);
add(comboBox);
setSize(100,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("actionPerformed");
//Why doesn't this call to setSelectedItem method fires an event
//and call actionPerfromed metho
comboBox.setSelectedItem("Item 3");
}
public static void main(String args[])
{
TestEvents testEvents = new TestEvents();
testEvents.comboBox.setSelectedItem("Item 2");
testEvents.comboBox.setSelectedItem("Item 1");
}
}