Re: Problem handling events on a JCombobox
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 30 Apr 2005 08:50:29 -0700
Luc Van Bogaert wrote:
Hi,
I'm using an editable JComboBox to start a search on a database table. In some cases, the combobox generates two action events (after Enter, and after a selection change) How can I avoid to process the same actionevent code (my search routine) twice?
Thanks!
Luc:
The JComboBox produces ActionEvents on two different actions. One is caused by selecting a new item the other by editing the field. Run my little program below and you will see. If you call setActionCommand("something") on the box that only effects the ActionEvent that is fired when a different item is selected not when you edit and/or press Enter in the editable part of the field. So if you don't want the event that is caused by pressing Enter throw away the event with the action command "comboBoxEdited".
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class test3 {
public static void main(String[] args) {
String[] items = {"item1","item2","item3"};
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox b = new JComboBox(items);
b.setEditable(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
System.out.println(ac);
}
});
f.add(b);
f.pack();
f.setVisible(true);
}
}
--
Knute Johnson email s/nospam/knute/ .
- References:
- Problem handling events on a JCombobox
- From: Luc Van Bogaert
- Problem handling events on a JCombobox
- Prev by Date: Chat program
- Next by Date: Re: Help with Java servlet : getServletContext().
- Previous by thread: Problem handling events on a JCombobox
- Next by thread: Help with Java servlet : getServletContext().
- Index(es):
Relevant Pages
|