• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out FocusListener
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the Text Prompt solution.
 
Frederick Winsnes
Greenhorn
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic