File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField behaviour when gaining focus" Watch "JTextField behaviour when gaining focus" New topic

JTextField behaviour when gaining focus

Xose Ramos

Joined: Sep 28, 2001
Posts: 1
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.

  • */
    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?
I agree. Here's the link:
subject: JTextField behaviour when gaining focus
It's not a secret anymore!