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?
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.