Skip to main content

Autocomplete combobox and FocusListener problem

2 replies [Last post]
msindoni
Offline
Joined: 2007-09-22

Hi to all,

I'm using autocomplete function on combobox (really great!). I've a little problem adding a FocusListener on comboboxes decorated with autocomplete function. In the FocusListener I change combo background color to yellow on focusGained and back to white on focusLost. On a 'normal' (=not autocomplete) combo this works fine, but if I use the autocomplete function combo doesn't change background color on focus gained.

Here my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

public class AutocompleteTest extends JFrame {

private JComboBox autocompleteComboBox;
private JComboBox normalComboBox;

public static void main(String args[]) {
try {
AutocompleteTest frame = new AutocompleteTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

public AutocompleteTest() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// List for combo boxes
String[] names = {"Mark", "Billy", "Rose", "William", "Susan" };

// Creates autocomplete combo, then adds focus listener
autocompleteComboBox = new JComboBox(names);
autocompleteComboBox.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0) {
// On focus gained, set background to yellow
autocompleteComboBox.setBackground(Color.YELLOW);
}
public void focusLost(FocusEvent arg0) {
// On focus lost, reset background to white
autocompleteComboBox.setBackground(Color.WHITE);
}
});
AutoCompleteDecorator.decorate(autocompleteComboBox);

// Creates normal combo, then adds focus listener
normalComboBox = new JComboBox(names);
normalComboBox.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0) {
// On focus gained, set background to yellow
normalComboBox.setBackground(Color.YELLOW);
}
public void focusLost(FocusEvent arg0) {
// On focus lost, reset background to white
normalComboBox.setBackground(Color.WHITE);
}
});

// Finally adds combos to form layout
getContentPane().add(autocompleteComboBox, BorderLayout.NORTH);
getContentPane().add(normalComboBox, BorderLayout.SOUTH);
}
}

Any help greatly appreciated

Bye, Marco

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Thomas Bierhance

Hi Marco!

You can observe this behaviour, because decorating the combo box makes it
editable. You would observe the same behaviour without decorating the combo box,
but making it editable: autocompleteComboBox.setEditable(true)

Editable combo boxes and focus listening is not an easy to solve problem, as the
behaviour varies for different L&Fs...

Try this code for editable combo boxes:

combBox.getEditor().getEditorComponent().addFocusListener(
new FocusListener(){
public void focusGained(FocusEvent arg0) {
comboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
}

public void focusLost(FocusEvent arg0) {
comboBox.getEditor().getEditorComponent().setBackground(Color.WHITE);
}
});

Best regards,
Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

msindoni
Offline
Joined: 2007-09-22

Hi Thomas!

Thank you very much for this workaround. It works well for my situation :-)

Sometimes I feel lost into Swing: I'm not new in java desktop programming, but there's always something to learn ;-)

Thanks a lot

Bye, Marco