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!

Problem Setting Focus on a JTextField

843806Apr 25 2008 — edited Apr 25 2008
I'm having some problems setting the focus to a JTextField, can anyone tell me what I'm doing wrong, or what the issue might be?

First some background about the application, just in case it helps. It was created using NetBeans and a Java Desktop Application template. The Component tree is as follows:
mainPanel--JPanel
   applicationTabPane--JTabbedPane
      tab1Panel--JPanel
      tab2Panel--JPanel
         selectionPanel--JPanel
            part1Button--JButton //(the button pressed)
         inputPanel--JPanel
            inputTextField--JTextField //(the component to get the focus)
      tab3Panel--JPanel
      tab4Panel--JPanel
The process leading to the text field getting the focus is this... The user selects the second tab and chooses a button to press, this fires an actionPerfomed, which calls a method that first locks the tabs so they can't be clicked again, hides the panel with the buttons and shows the panel with the text field, it is just after this that I try setting the focus with the following code
System.out.println("Is focasable " + inputTextField.isRequestFocusEnabled());
inputTextField.setRequestFocusEnabled(true);
inputTextField.requestFocus();
System.out.println("Has Focus " + inputTextField.isFocusOwner());
The code section above prints the following, and fails to set the focus to the required component.

Is focasable true
Has focus false

I have thought it might be something to do with actionPerformed needing to be returned before the components are displayed, and hence are not yet focasable, if this is the case I can't see a way of getting the focus without user intertaction, which is what I'm trying to avoid. Any help will be most welcome :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2008
Added on Apr 25 2008
4 comments
230 views