JTextField focus
Hello guys,
There is a thing I need help with. When I run my program, and the window shows, I want focus on the textField. Like Google, when you enter Google, you get focus on the search panel so you can search. My program got a simple search engine with a textField which I want focus on.
My program consists of tabs. This is an example for how I use it.
Main.java
Frame.java (extends JFrame). Here I add the tabs
Tab1.java (extends JPanel). This class contains the search engine, and I want to focus on the textfield.
I have tried:
textField.requestFocus(); on my Tab1.java file, but it won't get focused.
So I need some help from you. How should I do this?
Stjerne
Junior Poster in Training
53 posts since Sep 2011
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0
The JVM is probably moving the focus to where it wants it.
What component has focus when the app starts?
When does the code call requestFocus()?
That method is not recommended any more (see the API doc). It recommends you use another method: requestFocusInWindow
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
When the app starts, the first tab is focus. Nothing else on the first tab.
I call on the requestFocus() at the very last. First I add all the components. Then I set requestFocus() before I add actionListener. This is in the first tab class.
Stjerne
Junior Poster in Training
53 posts since Sep 2011
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0
Also, it looks like the tab is focused. Not the JPanel which the first tab extends.
requestFocusInWindow didn't work either..
Stjerne
Junior Poster in Training
53 posts since Sep 2011
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0
Try calling after the call to setVisible().
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
The thing is that I don't have any setVisible on my JPanel, but in the JFrame. I can't call the search textfield from my frame class, since it's on my panel..
Stjerne
Junior Poster in Training
53 posts since Sep 2011
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0
Edit: I managed to fix it :)
panelName.textField.requestFocus()
Stjerne
Junior Poster in Training
53 posts since Sep 2011
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0