File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "JTextField automatic selction, CellEditor" Watch "JTextField automatic selction, CellEditor" New topic
Author

JTextField automatic selction, CellEditor

Tilman Bender
Greenhorn

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
    Greenhorn

    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
    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?
    Tilman Bender
    Greenhorn

    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.

    Cheers,
    Mike Swingler
    Java Runtime Engineer
    Apple Inc.

    Thanks again Mike!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JTextField automatic selction, CellEditor
     
    Similar Threads
    JTable cell focus
    JTable customization
    Select vs Edit in JTable
    JTable Cell Renderers
    Problem with JTable