开发者

AWT Canvas cannot gain focus in the presence of another focusable component

I have a simple GUI with a JTextField and an AWT Canvas (to prevent the counter-question as to why I'm using an AWT Canvas: I need to have a window handle).

The Canvas is to process input events, that means it must be focusable. I assure this by using setFocusable(true) in its constructor, later checks using isFocusable() confirm that it is indeed focusable.

Now, the JTextField gains the focus by default when the GUI opens. That's fine by me so far. However, there is no way to get the focus away from that JTextField.

The article "The AWT Focus Subsystem" clearly states that if a focusable component is being clicked on, it will gain the focus. This does not happen, in fact, I receive zero focus change events whatsoever, only if the window gets deactivated and activated again, but then the focus is right back to the JTextField.

开发者_运维技巧

Explicit invocations of requestFocus() and requestFocusInWindow() do not help either, the latter always returns false.

I have gotten the same results with any focusable component if I replace the JTextField. If the Canvas is the only focusable container, everything works fine because it will always have the focus.

Am I missing something here? Is there any way I can make my Canvas gain focus in the presence of another focusable component, preferably without making that one unfocusable?


basically in swing focus gained 1st. left(ToRight) JComponents on the top

in most completed GUI, and if there (together with creating JComponents) are added Listeners to the JComponents, then these Listeners (f.e. Document) can take focus...

but works for me on startUp:

last lines in something class about JComponets .. 

myFrame.pack();
myFrame.setVisible(true); 
Runnable doRun = new Runnable() {

    public void run() {
        myComponent.grabFocus();
        myComponent.requestFocus();//or requestFocusInWindow
    }
};
SwingUtilities.invokeLater(doRun);


Sorry for leaving some info out that turned out to be the root of the problem.

As mentioned, I'm using a heavyweight component so I have a window handle. I need one because it is passed to an OpenGL application in a native library, the AWT canvas is then used as a rendering canvas.

In Windows, Java uses the GWLP_USERDATA window field to store a pointer to an AWTComponent object. However, said OpenGL application overrides that field to store its own Window object pointer, which will of course break all AWT related functionality.

I solved this problem by creating a custom window message handler that delegates incoming messages to both the OpenGL application and Java's AWT part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜