Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

Unable to set focus on the JTextField

Thread view: 
cybernerdsx2@yahoo.com - 05 Apr 2005 05:45 GMT
Hi,

I have a JTextField in a JPanel and I would like to have JTextField set
to be the default focus with the caret blinking in it.

I have tried requestFocusInWindow(), requestFocus(), grabFocus() and
setFocusable( true) and all failed.

How can I do that?
Arnaud Berger - 05 Apr 2005 07:45 GMT
Hi,

requestFocus should work, though.

see the following quick example :

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Focus extends JFrame {
public Focus(){
JPanel pan =new JPanel();
JTextField field1=new JTextField("11111111");
JTextField field2=new JTextField("222222222");
JTextField field3=new JTextField("333333333");
pan.add(field1);
pan.add(field2);
pan.add(field3);
setContentPane(pan);
pack();
setVisible(true);
field2.requestFocus();
}
public static void main(String[] args) {
new Focus();
}
}

Regards,

Arnaud
> Hi,
>
[quoted text clipped - 5 lines]
>
> How can I do that?
Kari Ikonen - 14 Apr 2005 20:52 GMT
> I have a JTextField in a JPanel and I would like to have JTextField set
> to be the default focus with the caret blinking in it.
>
> I have tried requestFocusInWindow(), requestFocus(), grabFocus() and
> setFocusable( true) and all failed.

First of all: Use requestFocus(), ignore those other methods.

Then quick quesses in the order of likehood:

(a) Call to requestFocus() is done *before* window is visible.
   => requestFocus() fails

(b) non-EDT-thread tries to call requestFocus()
   => requestFocus() fails

(c) Component is not yet attached into layout
   => requestFocus() fails

(d) Component is not visible
   => requestFocus() fails
 

Signature

KI



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.