Skip to main content

AutoCompleteDecorator prevent Activation of a KeyAdapter of a JComboBox

3 replies [Last post]
Currychicken
Offline
Joined: 2012-04-13
Points: 0

Hello!

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);
strictComboBox.addKeyListener(
new KeyAdapter() {

@Override
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);
filterSubjectModel.addElement(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.
kleopatra
Offline
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.

Cheers
Jeanette

kschaefe
Offline
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.

Currychicken
Offline
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.