views:

382

answers:

4

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?
+1  A: 

To modify the default behaviour, you can set the system property to false before initializing the UI components: System.setProperty("Quaqua.TextComponent.autoSelect", "false"); To modify a single component, you can use JTextField#putClientProperty("Quaqua.TextComponent.autoSelect", Boolean.FALSE);.

You can find other MacOS L&F specific properties here:

Quaqua Look & Feel - User Guide

jarnbjo
That's helpful, but it looks like Quaqua is a custom look and feel, not necessarily what my users are using.
amarillion
jarnbjo
+1  A: 

I noticed when looking through the JavaDocs that requestFocus() "is discouraged because its behavior is platform dependent." You should use requestFocusInWindow() instead and see if the same problem occurs with it.

requestFocusInWindow is part of the Focus subsystem, introduced in Java 1.4.

On a side note, the default Apple Look and Feel has at least one property in the apple.laf namespace: apple.laf.useScreenMenuBar

Edit: According to Sun, the Macintosh look and feel is only available on Macs.

R. Bemrose
amarillion
Unfortunately it turns out that requestFocusInWindow() has the same problem.
amarillion
+1  A: 

While using requestFocusInWindow() is indeed encouraged over requestFocus(), it still produces the same problematic behavior on Macs (e.g., highlighting of full text field).

One workaround I got to work was to explicitly set the cursor position after requesting focus:

JTextField.requestFocusInWindow();
JTextField.setCaretPosition(JTextField.getDocument().getLength() - 1);

Note the "-1" is necessary, otherwise it will continue to highlight the entire field.

I'm curious to know if this solution is platform independent. Does this screw up the desired Linux or Windows behavior?

AlexanderPico
Thanks for checking the requestFocusInWindow() behavior, as I don't have a Mac I'm flying blind here. Moving the caret position is not ideal, but perhaps it's possible to move the caret twice to get it back where it should be?
amarillion
A: 

A workaround might be (and I haven't tested this) to make the JList that inserts the variable names unfocusable. That way the focus will remain in the text field when you click on an item in the list. I'd recommend to use setRequestEnabled(false) on the JList, so that they are still focusable if you tab to them, but clicking them with the mouse will not focus them.

heycam