Loss of keyboard focus in Java appl running under linux

I have a small sample program that replicates my problem. When this program is run a window is created. If you select File->New another instance of the program window is created. Now if you try to go back and bring to front the first window, keyboard focus is not

transferred when run under linux. You can only type in the second window. The expected behavior does happen in Windows.

> uname -a

Linux watson 2.6.20-1.2933.fc6 #1 SMP Mon Mar 19 11:38:26 EDT 2007 i686 i686 i386 GNU/Linux

> java -version

java version"1.5.0_11"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)

Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

> javac -version

javac 1.5.0_11

import java.awt.event.*;

import javax.swing.*;

class SwingWindowextends JFrame{

SwingWindow(){

super("SwingWindow");

JMenuBar menuBar =new JMenuBar();

JMenu fileMenu =new JMenu("File");

JMenuItem newItem =new JMenuItem("New");

newItem.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

SwingWindow.createAndShowGUI();

}

});

fileMenu.add(newItem);

menuBar.add(fileMenu);

setJMenuBar(menuBar);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JTextField text =new JTextField(200);

getContentPane().add(text);

pack();

setSize(700, 275);

}

publicstaticvoid createAndShowGUI(){

JFrame frame =new SwingWindow();

frame.setVisible(true);

}

publicstaticvoid main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

[3081 byte] By [jltermana] at [2007-11-27 1:50:01]
# 1
You can implement the FocusListener interface. When the first JFrame gains focus, call text.requestFocusInWindow(). I hope this helps.
kmangolda at 2007-7-12 1:16:09 > top of Java-index,Security,Event Handling...
# 2

> You can implement the FocusListener interface. When

> the first JFrame gains focus, call

> text.requestFocusInWindow(). I hope this helps.

The call requestFocusInWindow is not helping, perhaps even making it worse.

The problem seems to be that I am in the situation where the call

KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner()

is returning the expected Component. The problem is that the KeyListener class that is registered with the Component is not being called when a key is being pressed.

The issue is that I have a component that has the keyboard focus, but the KeyListener class

is not responding.

This seems to be a linux only problem which makes it only mysterious.

jltermana at 2007-7-12 1:16:09 > top of Java-index,Security,Event Handling...