-
JComboBox Listener
Hi,
I've got a JComboBox on which i've set a listener.
The problem is that Java enter into the listener code whatever action is done on the combobox (state changed).
I would like Java enter into this code only when the active value of the combo box is changed (and not
during the selection, when the list is open).
Thanks a lot.
Sylvain
-
Re: JComboBox Listener
You'll have to be a bit more precise:
What kind of a listener did you add?
What do you mean by the 'active value' of the combobox?
Do you mean the selected item?
Do you mean you want to trap when characters are typed into the editbox?
Dave
-
Re: JComboBox Listener
Hi, thanks for answering. Here is my code:
main(){
...
combo.addActionListener(this) ;
...
}
...
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if(obj.equals(this.combo))
{
JComboBox combo = (JComboBox)obj;
String tempo = combo.getActionCommand();
System.out.println("# COMMAND = " + tempo);
}
...
}
The string tempo is always the same message.
I just want to trap the value when the user has selected one in the list, but only that. The fact is that
the action listener execute the code even when the user type some characters in the editbox....so each time
he type characters, code is executed....
Thanks.
Sylvain
-
Re: JComboBox Listener
If you just want notification of changes of the selected item, add an ItemListener:comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent event) {
int stateChange = event.getStateChange();
if (stateChange == ItemEvent.DESELECTED) {
System.out.println("Combo item deselected");
}
else if (stateChange == ItemEvent.SELECTED) {
System.out.println("Combo item "+ comboBox.getSelectedIndex() + " selected");
}
}
});
Dave
-
Re: JComboBox Listener
OK, i will try.
Thanks.
Sylvain.