I have the following Programm
package utests;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Foo extends JFrame {
private JComboBox combo;
public static void main(String... args) {
Foo f = new Foo();
f.showUI();
}
public void showUI() {
this.setVisible(true);
JPanel pane = new JPanel();
this.setContentPane(pane);
combo = new JComboBox();
combo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
System.out.println("You selected " + combo.getSelectedItem());
}
}
});
refreshBox();
pane.add(combo);
this.pack();
}
public void refreshBox(){
combo.removeAllItems();
ArrayList<String> list = new ArrayList<String>();
list.add("godsf");
list.add("södlkf");
list.add("ldsjlkfdsj");
for (String s : list) {
combo.addItem(s);
}
}
}
The programm will immediatly show you the string
You selected godsf
How can i check if the user checked something and not the program?
edit: i changed the programm a bit: The problem is that i need to refresh the box some times. Every Time i refresh the data in the box, the ItemListener is hit, and i get wrong results.
How can i check that a Action is done by the User and not by the Programm itself? To add some booleans seems to be not very Java-like…
Change the code of method refreshBox to code below:
does it meet your requirement?
I just change the code:
to the code: