KeyListener on JComboBox

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.Cheers, Adam
[210 byte] By [Naz027a] at [2007-10-3 4:02:28]
# 1

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

CeciNEstPasUnProgrammeura at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 2
You need to implement ItemListener interface in order to get events from, combobox void itemStatechanged(ItemEvent ie){ // need to implement this method}
javahelpera at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 3
No I dont need to implement an item change listener!!!! The ComboBox does have focus! Please read the question and dont answer it if you dont know what i'm talking about!!!!!
Naz027a at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 4

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>

Naz027a at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 5

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

CeciNEstPasUnProgrammeura at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 6
I'm sorry had a hard day. It doesn't work for me for some reason. Thanks anyway.
Naz027a at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 7
> I'm sorry had a hard day. It doesn't work for me for> some reason. Thanks anyway.No problem. You should still post in the Swing forum, with a link to this thread.
CeciNEstPasUnProgrammeura at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 8

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.

camickra at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...
# 9

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.

jchilvera at 2007-7-14 22:01:53 > top of Java-index,Java Essentials,Java Programming...