• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setting focus

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Imagine 2 rows and 2 colums
In row 1,I have one label(0,0) and text field(0,1).
In row 2, I have one label(1,0) and password field(1,1).
Can we able to set the focus to the password field(1,1)
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this a different problem to your other (very) recent post?

https://coderanch.com/t/344927/GUI/java/set-focus-password-field-swings
 
angelin prema
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is different.
If my passwordfield is the first field in my application can able to set focus.
But if it coes after the textfield,focus can't be set.
Have you got it?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
experiment with both ways mentioned in your other post
either
SwingUtilities.invokeLater
or
ComponentListener/componentShown
 
angelin prema
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya I have added component listener.
But it is working fine only when password field is the first field.
It is not working if i keep my password field as my second or third field in my screen.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just tested it (again) and componentListener works fine, the 2nd or 3rd
or whatever textField/passwordField starts off with the focus (java 1.6)

these are the basic rules you have to follow for focus
1)
use requestFocusInWindow() rather than requestFocus()
2)
requestFocusInWindow() will only work if the component is visible, so, if you have
pwd.requestFocusInWindow();
frame.setVisible(true);//where frame, in its hierarchy, has pwd
it won't work, it has to be
frame.setVisible(true);
pwd.requestFocusInWindow();

assuming you are still working with a JDialog (because of the passwordField),
and the dialog is modal, the above does not apply because once a modal dialog's
visibility is set to true, all code following is not executed until the dialog is disposed.

a componentListener will overcome the above problem, but it has to be added
prior to the dialog's setVisible(true) (for obvious reasons).

if you still have problems you will have to post the code you are trying,
so we can see what you are doing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic