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?

Recommended Answers

All 6 Replies

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

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.

Also, it looks like the tab is focused. Not the JPanel which the first tab extends.

requestFocusInWindow didn't work either..

Try calling after the call to setVisible().

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..

Edit: I managed to fix it :)

panelName.textField.requestFocus()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.