File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Choosing which control has focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Choosing which control has focus" Watch "Choosing which control has focus" New topic
Author

Choosing which control has focus

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
How do I choose which control has focus when a window is opened. Typically, I create most GUI controls in a JPanel subclass. I know I can call the requestFocus() method to move focus to a chosen control. However, putting this in the JPanel's constructor doesn't work. Where should I place the call to requestFocus()?

Layne


Java API Documentation
The Java Tutorial
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
just a guess

i played with some Hi Lo gui i just made, and worked:


java amateur
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thanks for the suggestion. There are a couple of problems here. First of all, it is considered bad practice to create public member fields like gui.button and gui.logica in your example.

Second, when I write GUI programs, I typically only have one line in main():

Then the MyFrame class typically creates a menu and a MyPanel (for example). The actual controls are created in MyPanel. This is probably the main difficulty for my question. There is no way to directly access the components. I tried overriding setVisible() in the MyPanel class, but it is never called.

Even without this particular situation, the same problem arises with other dialog boxes that are not displayed when the program starts. So I'm looking for a more general solution that will solve this problem in just about any situation. Keep in mind that the controls are typically declared as private fields so external access is prohibited.

Layne
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i found (but didnt read) this this article

original url had 1.4 but i changed it to 1.5

hope this helps
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thanks. I'll take a look at it to see if it helps.

Layne
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i tried, inside frame's constructor:
and worked fine; i got the idea from this except:
Because Component.requestFocus() cannot be implemented consistently across platforms, developers are encouraged to use Component.requestFocusInWindow() instead.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thanks for the suggestion. I'll have to see if it also works in a JPanel's constructor.

Layne
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
A component can also be given the focus programmatically, such as when its containing frame or dialog is made visible.

this excert is from sun java tutorial
so,
botao.requestFocusInWindow();
will only work after call to setVisible(), and that's why you cant get it working inside your panel's constructor, i guess
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Unfortunately, I'm not at my development machine at the moment and I don't have time to look into it further right now. The suggestions you have given so far should help. I'll let you know what I find out.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Choosing which control has focus