File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Choosing which control has focus

 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a guess

i played with some Hi Lo gui i just made, and worked:
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'll take a look at it to see if it helps.

Layne
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion. I'll have to see if it also works in a JPanel's constructor.

Layne
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic