This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Mac OS and the fly likes JTextField automatic selction, CellEditor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "JTextField automatic selction, CellEditor" Watch "JTextField automatic selction, CellEditor" New topic

JTextField automatic selction, CellEditor

Tilman Bender

Joined: Mar 16, 2008
Posts: 11
Hi there,

I am currently working on an application that displays data in a JTable.
I have to meet severeal UI-Requirements such as:

  • start editing on doubleklick (select all cell content, if any)
  • start editing on pressing enter (select all cell content, if any), when a cell is selected
  • start editing when user starts typing while a cell is selected

  • I managed to fit these requirements on windows by doing the following:

    1. start editing on doubleklick (select all cell content, if any)Windows does (apparently) not do a automatic selectAll() for the cell content when you doubleclick a cell to start editing. This can be fixed by overriding shouldEditCell

    2.start editing on pressing enter, when a cell is selected
    For this one I had to change JTable's default behavior to move down one cell on ENTER:
    In the code that creates my JTable:

    Manage the selectAll() in my editor:

    So far so good. The last requirement is the one that causes me a headache:
    On windows it works with the code I already have above.
    On Mac OS X 10.5 the following problem occurs:

    1.I got to the cell
    2.I type 'a'
    3.editor is activated, 'a' is filled into the TextComponent (I use JFormattedTextField), but it is selected and therefore overwritten by the following characters, when I type 'bc'.

    Does anyone know how I can fix this? Any properties to switch or something?

    [ June 30, 2008: Message edited by: Tilman Bender ]
    [ June 30, 2008: Message edited by: Tilman Bender ]
    Tilman Bender

    Joined: Mar 16, 2008
    Posts: 11
    Additionally I found something in the Java Release Notes for Leopard:

    Radar #4890361

    JTextFields did not match the native focus/selection behavior of Mac OS X

    JTextFields now 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.

    This problem is now fixed in Mac OS X 10.5.

    Does anyone know how to access that Bug-Tracker? Is it closed? Do I need a special ADC Membership?
    Tilman Bender

    Joined: Mar 16, 2008
    Posts: 11
    Oh how I love answering myself!
    The solution to this turns to be quite easy, once you know it:

    You can override this behavior with the following: textfield.setCaret(new DefaultCaret()). We install a custom Aqua caret in every text field to modify the default selection behavior provided by Swing to match the native behavior of Mac OS X text widgets. If you are doing any sort of advanced selection manipulation, you might want to look into installing your own custom subclass of DefaultCaret as well.

    Mike Swingler
    Java Runtime Engineer
    Apple Inc.

    Thanks again Mike!
    I agree. Here's the link:
    subject: JTextField automatic selction, CellEditor
    It's not a secret anymore!