Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac L&F problems: Differing behavior of JTextField.requestFocus()

I have a problem with JTextField.requestFocus() behavior that appears to be different on Mac OS X.

Here is my situation: I have a dialog with a JList and a JTextField. The user is supposed to write a boolean expression in the text field, and the list contains the names of all the variables that might be entered in the expression. Because the user is expected to continue entering the expression after clicking on a variable from the list, the program helpfully calls JTextField.requestFocus(). This way you can e.g click "pvalue" from the list and then type " < 0.05" without the need to click on the textfield in between.

This all works fine on my development machine (Linux), but I got a bug report from a Mac user that clicking on the list actually selects all text in the text field, making it easy to accidentally overwrite what was entered before.

I suspected this is a problem with the Mac look-and-feel, after some searching it seems that indeed there is a "Quaqua.TextComponent.autoSelect" property for the mac look-and-feel that seems to be related to this problem: http://www.randelshofer.ch/quaqua/guide/jtextcomponent.html

My general question is:

  • Can you suggest a workaround for this problem?

In case that is too broad, an answer to these subquestions would already be a big help:

  • A possible solution could be to change the property "Quaqua.TextComponent.autoSelect". How do I do that?
  • I'm not even sure what "Quaqua" is. It looks like it is a customized look and feel. What is the default look and feel for Mac OS X? Does it have a property similar to Quaqua.TextComponent.autoSelect?
  • Is there a possibility to tweak look and feel for a single component instance only? If so, how?
  • Is it possible to set the Mac look and feel on my Linux development machine so that I can actually confirm this bug (all the above is really based on hunches and suspicions)? If so, how?
like image 619
amarillion Avatar asked Dec 14 '22 02:12

amarillion


1 Answers

Seems this is a bug of Mac OS. JTextFields select their contents when they gain focus though keyboard tab cycling. If the insertion point is in the middle of the text, the insertion point will remain and the entire text will not be selected.

As a workaround you can override this behavior with the following, it works fine for me:

textfield.setCaret(new DefaultCaret()).

More details you can refer to this and this.

like image 117
Aaron Avatar answered Feb 15 '23 09:02

Aaron