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?
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.