Skip to main content

AutoCompleteDecorator prevent Activation of a KeyAdapter of a JComboBox

Please note these forums are being decommissioned and use the new and improved forums at
3 replies [Last post]
Joined: 2012-04-13
Points: 0


I want to use a JComboBox with the SwingX AutoCompleteDecorator to select subjects to filter a JXTable. An Element from the JComboBox should be selected by the ENTER-Key but the KeyAdapter does not work, when the AutoCompleteDecorator is active. How can I solve this problem?

I am looking forward to you replies!

strictComboBox = new JComboBox();
Subjects subjects = Subjects.getInstance();
list = subjects.getSubjects();
strictComboBox.setModel(new ListComboBoxModel<String>(list));
// AutoCompleteDecorator.decorate(strictComboBox);
new KeyAdapter() {

public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_ENTER) {
String str = (String) strictComboBox.getSelectedItem();
if (filterSubjectModel.contains(strictComboBox.getSelectedItem()) == false) {
System.out.println("add to subject filter: " + str);
} else {
System.out.println("do not add to subject filter = " + str);
panel.add(strictComboBox, c);

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-11
Points: 0

basic Swing rule: don't use low-level event listeners like f.i. KeyListener ever, especially not for compound components, and even more so for compound components which do some internal magic :-)

Instead, use semantic listeners, like f.i. DocumentListener on the editor's textField or ItemListener on the combo. Which might be a bit problematic with a AutoCompleteDecorator, as it does some magic with that as well, never tried.


Joined: 2006-06-08
Points: 0

We should have removed all of the notification for the magic of autocomplete from the client's knowledge. However, an ActionListener (or ItemListener, if you have to) is probably the right thing here.

Joined: 2012-04-13
Points: 0

Thanks for your answer. Do you have a recommendation for an introduction, example or manual in the web regarding your point "don't use low-level event listeners"?

I am a newbie and needs some more information.