The Source for Java Technology Collaboration
User: Password:



Evan Summers

Evan Summers's Blog

Tipsy Snipsy: Forward Focus Traversal

Posted by evanx on March 15, 2007 at 04:59 AM | Comments (5)

As you've noticed, when you press Enter on fields like JTextField et al, an ActionEvent is generated, and focus is not transferred to the next field in the focus cycle.

But what if you are not interested in ActionEvent's on fields, and would rather Enter have the same effect as Tab, maybe because your users are used to having to enter data with the Enter key?

    protected void setDefaultFocusTraversalKeys() {
        Set set = new HashSet(
                KeyboardFocusManager.getCurrentKeyboardFocusManager().
                getDefaultFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        set.add(KeyStroke.getKeyStroke("ENTER"));
        KeyboardFocusManager.getCurrentKeyboardFocusManager().
                setDefaultFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
    }   

But then we aint gonna get those ActionEvent's no more, which may or may not be a problem.


References

Setting Focus Traversal Keys for the Entire Application on JavaAlmanac.com.

Using the Swing Focus Subsystem on the Java Tutorial.



Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Most of my apps I dont want to go to the next field if the user presses enter as they are forms to submit either search criteria or save a form entry.
    On JLists I often need to override enter it going to the next selection - I want it to kick off an event


    But the code is useful - does it apply to a JTable - I hope so as it drives me nuts that I cant tab out of a table under certain conditions (like last cell in the table. Its been a struggle as I get stuck somewhere (maybe the scrollpane or enclosed panel)
    Last question, I thought I read an article a while back from Santosh Kumar who noted a memory leak if you are not careful when registering this way, I think he added weak listeners, I'll see if I can find it

    Posted by: aronsmith on March 16, 2007 at 10:30 AM

  • no, it doesn't seem to work for JTable

    Posted by: evanx on March 16, 2007 at 03:40 PM

  • I've been hacking around for several years now on a JTableForEdit extention to JTable (JXTable to be exact) that does exactly what you mention here: move to the next cell upon enter. This is because data entry often is done using the numeric keypad.

    The component is used in production and seems to work fine.

    Posted by: tbee on March 17, 2007 at 12:59 PM

  • tbee, that sounds useful, where can i get the source?

    Posted by: evanx on March 18, 2007 at 05:31 AM

  • I've looked into similar problems in the past, and you might find the following links relevant:

    Skipping an empty JTable when tabbing
    JTextArea: navigating and/or typing TAB

    One of the shortcomings is that you don't really have a proper focus model inside of a JTable. You can listen for row/column selection changes, but the change of row and the change of column are delivered separately so it's difficult to determine if you're dealing with the "tab out of the last cell scenario" (and that assumes keyboard navigation, ignoring mouse clicks).
    - Chris

    Posted by: chris_e_brown on March 20, 2007 at 05:43 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds