3

This code calls a routine when enter is pressed in a JTable (called gametable). It works well, but I would like the same Action to be called when moving up or down in the JTable without the need for pressing enter; I can't get it to work. I tried substituting VK_ENTER with VK_UP, but I am unable to move up and down the table?

KeyStroke enter = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0);

gameTable.getJTable().unregisterKeyboardAction(enter);
gameTable.getJTable().registerKeyboardAction(new ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            synchronized (this) {
                gotoGame(gameTable.getSelectedIndex());
            }
        }
    }, enter, JComponent.WHEN_FOCUSED);

I can't figure it out. Can someone help me?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1432365
  • 103
  • 1
  • 1
  • 7

2 Answers2

3

You'll have to separate the steps:

  1. First put two KeyStroke instances in the InputMap so they target the same actionMapKey:

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
    String actionMapKey = "anActionMapKey";
    gameTable.getInputMap().put(enter, actionMapKey);
    gameTable.getInputMap().put(up, actionMapKey);
    
  2. Then associate that actionMapKey with your Action:

    gameTable.getActionMap().put(actionMapKey, new AbstractAction(actionMapKey) {
        ...
    });
    

See How to Use Actions and Key Bindings for details.

I am wary of your use of synchronized (this) in this context; you should be constructing your GUI on the event dispatch thread.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Cheers for this insight. I've read through your example and read your links on actions and bindings. I now know that this is the best way forward - just gotta put it all together. – user1432365 Aug 11 '12 at 21:43
0

You need to add a keylistener to your JTable. Then in your key listener you can check for any button pressed including Enter and take the same action.

I have a program with similar code. Here I just display different values in one textarea if the arrow key choose a different cell, but I think it may give you an idea of how to set it up.

import java.awt.event.KeyEvent;

import javax.swing.JTable;

public class MyClass {
    static JTable table = new JTable();

    public static void main(String[] args) {
        table.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(final java.awt.event.KeyEvent evt) {
                tableKeyReleased(evt);
            }
        });
    }

    private static void tableKeyReleased(final java.awt.event.KeyEvent evt) {
        final int key = evt.getKeyCode();
        if (key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN
                || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT) {
            final int row = table.getSelectedRow();
            final int column = table.getSelectedColumn();

            final Object cellValue = table.getValueAt(row, column);

            if (cellValue == null) {
                return;
            }

        }
    }
}
Logan
  • 2,369
  • 19
  • 20
  • `KeyListener` is not intrinsically _wrong_, but the question appears to concern key bindings. – trashgod Aug 11 '12 at 18:45
  • Maybe I misunderstood the question then. Functionally it sounds like what I'm doing with my program. I do have some keybindings too for shortcuts and copy/paste, but that's a different type of functionality. – Logan Aug 11 '12 at 18:57
  • `KeyListener` is notorious for "missing" keys. If the `KeyEvent` is consumed higher in the event handling chain, the `KeyEvent` most likely won't make it to you. KeyBindings are much more reliable. IMHO – MadProgrammer Aug 11 '12 at 21:21