• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

setting focus

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
is this a different problem to your other (very) recent post?

http://www.coderanch.com/t/344927/GUI/java/set-focus-password-field-swings
 
angelin prema
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!