> I get no response when adding a KeyListener to a
> JComboBox. I believe this is because I need to add
> the listener to the comboBox's editable component. Am
> I right and how do I do this.
I rather think it's because the JComboBox doesn't have the focus when you hit a key...
I managed to fix it myself using this code:
JTextField editor = (JTextField) combo.getEditor()
.getEditorComponent();
editor.addKeyListener(this);
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==e.VK_ENTER) {
String search = getString();
Boolean found = false;
int m=0;
for (int n=0; n<combo.getItemCount(); n++) {
if (!found) {
String s = combo.getItemAt(n).toString();
try {
if (s.startsWith(search)) {
found = true;
m = n;
}
}
catch (Exception ex) {}
}
}
combo.setSelectedIndex(m);
}
}
But for some reason the listener only fires if I press Enter twice!? Does anyone know why?
Cheers>
> The ComboBox does have focus!
You didn't tell.
> and dont answer it if you dont know what i'm talking about!!!!!
You didn't say much except "doesn't work". You didn't post your code. You also didn't post post a Swing-related question in the Swing forum where all the Swing experts are. What do you expect?
import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class TestSomething {
public static void main(String[] sgra) {
new TestSomething();
}
TestSomething() {
JFrame frame = new JFrame();
JComboBox box = new JComboBox();
KeyListener listener = new KeyListener() {
public void keyTyped(KeyEvent keyEvent) {
System.out.println("Typed: " + keyEvent.getKeyChar());
}
public void keyPressed(KeyEvent keyEvent) {
System.out.println("Pressed: " + keyEvent.getKeyChar());
}
public void keyReleased(KeyEvent keyEvent) {
System.out.println("Released: " + keyEvent.getKeyChar());
}
};
box.addKeyListener(listener);
frame.getContentPane().add(box);
frame.setVisible(true);
}
}
Works perfectly well for me.
Simple answer really. I was all set to answer when I realized that:
a) I've already suggested to several times that Swing related questions should be posted on the Swing forum. Since you don't listen to advice given why should I waste my time helping again
b) you rarely take the time to respond to your questions indicating whether the advice given was helpfull or not. Again why should I help without any feed back as to whether my suggestions are helping or not?
http://forum.java.sun.com/thread.jspa?threadID=749841
http://forum.java.sun.com/thread.jspa?threadID=750047
http://forum.java.sun.com/thread.jspa?threadID=753963
http://forum.java.sun.com/thread.jspa?threadID=754013
http://forum.java.sun.com/thread.jspa?threadID=754843
Thats not to mention your attitude to others in this posting who tried to help but misread the question. What goes around comes around.
You are right. This is because you need to add the key listener on the component that the combo box is using to service the editing.
ie
JTextComponent editor = (JTextComponent)urCombo.getEditor().getEditorComponent();
editor.addKeyListener( new KeyAdapter() {
public void keyReleased(KeyEvent evt)
{
// ur code
}
});
Enjoy.
Jez.