wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to make all the text in a JField selected when JField is in focus? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make all the text in a JField selected when JField is in focus?" Watch "How to make all the text in a JField selected when JField is in focus?" New topic
Author

How to make all the text in a JField selected when JField is in focus?

Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

Hello
I've been using a JFrame containing some JFields in which the user should input text or numbers.
I want the the text in the JFields to be selected when the user clicks in the field but I don't know how to.
(So far I've been able to make the text selected when double-clicked by using _ instead of spaces, but that is quite ugly to look at)


I was thinking that I should use a Listener and the selectAll()-method in some way, but which Listener should I use?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

Check out FocusListener


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2156
    
    7
Check out the Text Prompt solution.
Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

The TextPrompt solution was exactly what I was looking for! I wonder why this is not a part of the official API.
Thank you for your answers!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Frederick Winsnes wrote:I wonder why this is not a part of the official API.

Because Rob C wrote it himself

Gems like these rarely make it into the official API because in most cases, Oracle doesn't even know they exist. Sometimes they do though; I've written my own java.io.FileFilter implementation that looks for extensions, plus a generic java.io.FileFilter to javax.swing.filechooser.FileFilter bridge, long before Sun added FileNameExtensionFilter (not based on my classes by the way, the idea is just common). And don't forget about the upcoming java.util.Objects class in Java 7 that finally adds an equals utility method that can compare two objects references which may or may not be null. Oh, and those Objects.nonNull methods? I've had those methods for years already. (Again, it's not copied from my classes. They're not even public.)

But in general, a lot of useful code never makes it into the official API. Fortunately, it doesn't have to; you can simply download it and include it in your project, usually as JAR files.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to make all the text in a JField selected when JField is in focus?
 
Similar Threads
Can I use repaint() on a JFrame?
Issue with selection of text when textfield component gains focus.
grouping labels and buttons in a JPanel with other JPanels
JTable: highlight text of cell
Modified text in JTextField not returning