tags:

views:

237

answers:

2

I've found several pages and SO answers about the enter-as-tab problem in Java, but all propose either overriding methods of JTextField or adding a key listener to every component.

But isn't there any other way? Can't I override something of the LookAndFeel or install some global policy?

+1  A: 

you can probably use http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html

to change the keyBinding for the enter key

or you can add focustravesal keys

setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, your keys here);
Peter
Both methods still require touching each component instance.
DR
A: 

After some documentation crawling I found a solution:

// Get default keys
Set<AWTKeyStroke> ftk = new HashSet<AWTKeyStroke(
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .getDefaultFocusTraversalKeys(
        KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

// Add our key
ftk.add(KeyStroke.getKeyStroke("ENTER"));

// Set new keys
KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .setDefaultFocusTraversalKeys(
        KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, ftk);

This adds the enter key to the list of keys which are used for forward traversal. (Backward traversal similar)

DR