Incorrect and permanent loss of keyboard focus



I have a small sample problem that replicates my problem. The program
creates a window with a text field that has keyboard focus.

The window has a menu item that I use to create another window of that
type that correctly gets the keyboard focus.

I then click on the old window, bring it to front. It does not get the
keyboard focus like it should in Linux. It will work correctly in windows.

I tried adding the call requestFocusInWindow which did not help and led
to unpredictable behavior. 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 more mysterious.

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 SwingWindow extends JFrame {

SwingWindow() {
super("SwingWindow");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
newItem.addActionListener(new ActionListener() {
public void 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);
}

public static void createAndShowGUI() {
JFrame frame = new SwingWindow();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
.



Relevant Pages

  • Re: Incorrect and permanent loss of keyboard focus
    ... The window has a menu item that I use to create another window of that type that correctly gets the keyboard focus. ... The problem is that the KeyListener class that is registered with the Component is not being called when a key is being pressed. ... public static void main{ ...
    (comp.lang.java.gui)
  • Re: Keyboard loses focus - VS2003
    ... My workflow is also interuped when searching. ... ESC to close the find window. ... you lose keyboard focus from your source file to the Output Window. ... > So I hit Escape to switch back to my source file. ...
    (microsoft.public.vsnet.ide)
  • Re: unexpected beeps from edit control
    ... > I have multiple edit controls in a non-dialog window, and I want to use the ... > tab key to cycle the keyboard focus between them. ... > except that every time the tab key is pressed there is a beep. ...
    (microsoft.public.win32.programmer.ui)
  • Re: Previous Instance
    ... >active but have no keyboard focus. ... It seems the window with the icon keeps the focus. ... >instance is minimized (i.e. at the bottom of the z-order) and you have other ...
    (microsoft.public.vb.winapi)
  • Re: So many Forms Issue Still not solved :(
    ... Window Message is a good idea, check out this link, you can catch the ... public string UniqueID ... private void BaseFormCommSuiteMobile_Load(object sender, EventArg= ... public static void AddForm ...
    (microsoft.public.dotnet.framework.compactframework)