Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

When does requestFocusInWindow() fail

843804Dec 13 2004 — edited Oct 15 2007
Hello,

I'm trying to set the focus on a class derived from JPane() with requestFocusInWindow() and the compoinent never gets the keyboard focus, unless I click on the pane itself. Is there somewhere a good document that explains when requestFocusInWindow() will work and when not.

Thanks in advance

Marc

Comments

843804
Not sure what a JPane() is, but if it's derived from a JInternalFrame you may need to setSelected() first. Check this out:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=507860

Maybe it will help.
800382
you call setFocusable(true) on the panel first?
camickr
You can't set the focus on any component unless its visible.
843804
I have just read in the documentation that requestFocusInWindow() will only give focus to the component when the frame containing the frame is displayed, this is what actually is happening with my application, if I use the window manager to hide/unhide my window the component get the focus. Apparently requestFocusInWindow() just set a kind of default focussed component for when the window containing the component gets activated.
Am I correct, that if you want to force the focus on one component is to use the FocusTraversable policy and focusNextComponent(), focusPreviousComponent(), downFocusCycle() or upFocusCycle() (if at least you want to avoid to activate/deactivate your window. I hope there is a easier way to set the focus on one of the components in a frame.

Marc
843804
Also check if you added the keyListener to the component - and if component extends KeyAdapter or implements KeyListener, add:

addKeyListener(this);

(it seems focus can't be gained if there is no KeyListener in the component)
843805
I see the same problem.
Is it necessary to add a KeyListener ?
It sounds very strange.
Anyway hiding/unhiding the window will show the correct focus location.
I tested also using requestFocus but the behaviour is the same.
Could someone help?
Tks
Tullio
794342
try putting the request in a swingUtilities
SwingUtilities.invokeLater(new Runnable(){
  public void run(){
    [component].requestFocusInWindow();
  }
});
843805
I already tried butnothing changed.
Tks
Tullio
843805
requestFocusInWindow() is crap and just doesn't work consistently. Use requestFocus() instead. I know that's not what the documentation reads, but it works.
843806
This works:

addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent ce) {
securePanel.tfUsername.requestFocusInWindow();
}
});

- Chris Murphy (www.strandz.org)
843806
I had the same problem with the request. This works too:

public void windowOpened(WindowEvent e){ component.requestFocus(); }
1 - 11
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 12 2007
Added on Dec 13 2004
11 comments
1,247 views