KeyListener won't work in Java 1.6

Hi,

I have a application (TN5250j) that opens a JFrame which contains several buttons. When clicked on one of these buttons, a JOptionPane opens as dialog with a message and a close button. The message (KeyGetter) is a descendant of JLabel which contains a KeyListener to verify the keys pressed by the user. This is used to let the user set their own accelerators to a specific action.

When running in JRE 1.5.0_06, everything works fine and i' ve logging of the pressed keys. In JDK 1.6.0_01, the keylistener doesn't seem to react. First I thougt it was a focus problem, but I've been debugging and the focus is in both Java's the same.

publicclass KeyGetterextends JLabel{

KeyEvent keyevent;

public KeyGetterInterface(){

super();

addKeyListener(new KeyAdapter(){

publicvoid keyTyped(KeyEvent e){

System.out.println("KeyTyped");

}

publicvoid keyPressed(KeyEvent ke){

System.out.println("KeyPressed");

}

publicvoid keyReleased(KeyEvent e){

System.out.println("KeyReleased");

}

});

}

publicboolean isFocusTraversable (){

returntrue;

}

/**

* Override to inform focus manager that component is managing focus changes.

* This is to capture the tab and shift+tab keys.

*/

publicboolean isManagingFocus(){

returntrue;

}

}

Thanx a lot

CuscoAdvan

[2634 byte] By [CuscoAdvana] at [2007-11-27 4:46:56]
# 1

It seems to work okay for me. When the message dialog appears, I have to press TAB once to get the focus off the "OK" button. After that, all key events are intercepted by the label, except "Enter", which dismisses the dialog. The behavior is exactly the same under 1.5.0_09 and 1.6.0_01. What are you doing differently? import java.awt.event.*;

import javax.swing.*;

public class Test extends JLabel {

KeyEvent keyevent;

public Test(String str) {

super(str);

addKeyListener(new KeyAdapter() {

public void keyTyped(KeyEvent e) {

System.out.println("KeyTyped");

}

public void keyPressed(KeyEvent ke) {

System.out.println("KeyPressed");

}

public void keyReleased(KeyEvent e) {

System.out.println("KeyReleased");

}

});

}

public boolean isFocusTraversable () {

return true;

}

/**

* Override to inform focus manager that component is managing focus changes.

* This is to capture the tab and shift+tab keys.

*/

public boolean isManagingFocus() {

return true;

}

public static void main(String... args)

{

System.out.println(System.getProperty("java.version"));

final JFrame frame = new JFrame("test");

JButton btn = new JButton("Press me!");

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

JOptionPane.showMessageDialog(frame, new Test("Type me!"));

}

});

frame.add(btn);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 300);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

By the way, you'll have much better odds of getting useful feedback if you provide complete, runnable sample code from the beginning.

uncle_alicea at 2007-7-12 9:59:35 > top of Java-index,Java Essentials,Java Programming...
# 2

I've noticed that in Java 1.5 the button of the optionpane is set as default button, but not focused. In Java 1.6 the button is set as default and focused. Something must have changed in JOptionPane I think in 1.6.

The only difference I can see between the code is that I use createDialog() to show the optionpane instead of showMessageDialog().

BTW, thanx for the tip

CuscoAdvan

CuscoAdvana at 2007-7-12 9:59:35 > top of Java-index,Java Essentials,Java Programming...
# 3
I've found a solution for the problem.When created the JOptionPane, I add a ComponentListener to it. In the ComponentShown, I do a requestFocus() for my JLabel and everything works fine in Java 1.5 and 1.6CuscoAdvan
CuscoAdvana at 2007-7-12 9:59:35 > top of Java-index,Java Essentials,Java Programming...