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 / August 2004

Tip: Looking for answers? Try searching our database.

Selecting all the text in a JTextField

Thread view: 
Alan Rubin - 22 Aug 2004 11:01 GMT
The default behaviour when focus passes to a JTextField appears to be
for the cursor to go to the end of the text.

I would like to set it up so that all the text is selected and typing
anything will erase the old text.

How do I do this?

Thanks.

Alan
Filip Larsen - 22 Aug 2004 12:16 GMT
> The default behaviour when focus passes to a JTextField appears to be
> for the cursor to go to the end of the text.
>
> I would like to set it up so that all the text is selected and typing
> anything will erase the old text.

If you have a member field like

 private JTextField text = new JTextField();

you can add a focuslistener to selected the text like

 text.addFocusListener(new FocusAdapter() {
  public void focusGained(FocusEvent e) {
   text.selectAll();
  }
 });

If you have a lot of text fields, you can of course do this from a
central builder, a visitor or in a subclass constructor.

Regards,
Signature

Filip Larsen

Alan Rubin - 23 Aug 2004 14:48 GMT
>   private JTextField text = new JTextField();
>
[quoted text clipped - 5 lines]
>    }
>   });

Thanks. What I have written in focusGained is:

((JTextField)e.getComponent()).selectAll();

Alan
Tor Iver Wilhelmsen - 22 Aug 2004 14:37 GMT
> I would like to set it up so that all the text is selected and typing
> anything will erase the old text.

In addition to the answer you got, this was IIRC a change in the
Windows L&F because Windows controls stopped automatically selecting
text on focus in W2k or so.


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.