This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JTextField behaviour when gaining focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField behaviour when gaining focus" Watch "JTextField behaviour when gaining focus" New topic
Author

JTextField behaviour when gaining focus

Xose Ramos
Greenhorn

Joined: Sep 28, 2001
Posts: 1
Hello,
I would like to post a question concerning the behaviour of JTextField when it gains focus. I believe it is a kind of bug in the implementation, but all the research I made on the topic has not given me any further information.
Everything started because of a requirement: The text fields in our applications should select the contents when they get the focus. (Easy to see, when a field gets the focus, text should appear with a green background).
This seemed to be so easy because this is the default behaviour of the Windows Look and Feel. This is what is said in the WindowsTextFieldUI class:
/**
* Provides the Windows look and feel for a text field. This
* is basically the following customizations to the default
* look-and-feel.
*

    *
  • The border is beveled (using the standard control color).
    *
  • The background is white by default.
    *
  • The highlight color is a dark color, blue by default.
    *
  • The foreground color is high contrast in the selected
    * area, white by default. The unselected foreground is black.
    *
  • The cursor blinks at about 1/2 second intervals.
    *
  • The entire value is selected when focus is gained.
    *
  • Shift-left-arrow and shift-right-arrow extend selection
    *
  • Cntrl-left-arrow and cntrl-right-arrow act like home and
    * end respectively.
    *

  • */
    Unfortunately:
    I have found out in Sun JVM this was not working at all (JVM 1.2.2, JVM 1.3.1, JVM 1.4b). But on a IBM JVM (JVM 1.2.2) it worked fine.
    This was somehow confusing me completely. An anouced behaviour is later not working in the implementation. Why?
    I solved the problem by simply calling the WindowsTextFieldUI specifically and assigning it to the JTextFields. I did not rewrote any code, I just simply made sure the class was being used.
    Does anybody knows about this problem? Is that a problem with the Windows Look and Feel? Should I notify Sun about this?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTextField behaviour when gaining focus