Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Frederick Winsnes
Greenhorn
Posts: 17
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out FocusListener
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Text Prompt solution.
 
Frederick Winsnes
Greenhorn
Posts: 17
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic