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 ]
Joined: Mar 16, 2008
Additionally I found something in the Java Release Notes for Leopard:
JTextFields did not match the native focus/selection behavior of Mac OS X Description:
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. Resolution:
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?
Joined: Mar 16, 2008
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.
Cheers, Mike Swingler Java Runtime Engineer Apple Inc.