When i try to perform action for comboBox_0, it wil print "red" and change selected item on comboBox_1 as "apple", however it will also generate action event for comboBox_1 and will also print "blue".
How can i perform this so the program will not see changing comboBox_1 item as generating action event. i dont need it to print "blue", only print "red" and change to "apple".
Is there a way for java to differentiate between user conducted action vs the program itself?
Sorry, i'm new to this field and i cant check source where they discuss about ActionEvents.
here's the example:
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==comboBox_0)
{
System.out.println("red");
comboBox_1.setSelectedItem("apple");
}
else if(e.getSource()==comboBox_1)
{
System.out.println("blue");
}
else if(e.getSource()==comboBox_2)
{
System.out.println("green");
}
}
You are raising an event, but at the same time you don't want to use it. Enter the flag into your program. Use this:
private boolean flag = false;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==comboBox_0)
{
System.out.println("red");
comboBox_1.setSelectedItem("apple");
flag = true;
}
else if(e.getSource()==comboBox_1)
{
if(flag){
flag = false;
return;
}
System.out.println("blue");
}
else if(e.getSource()==comboBox_2)
{
System.out.println("green");
}
}
Unfortunately, it is not possible to distinguish the event source (software or user-defined). But it is not necessary. If there were such an implementation, it would in any case be reduced to a"flag". You can use boolean or Emun as the flag.