-
November 9th, 2001, 12:09 PM
#1
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
Car devastator !
-
November 9th, 2001, 12:24 PM
#2
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 12th, 2001, 03:27 AM
#3
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
Car devastator !
-
November 12th, 2001, 07:15 AM
#4
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 12th, 2001, 10:57 AM
#5
Re: JComboBox Listener
OK, i will try.
Thanks.
Sylvain.
Car devastator !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|