Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
    Bookmark Topic Watch Topic
  • Mark post as helpful
  • send pies
  • Report post to moderator
Have you struggled with launching some kind of a dialog when the user presses ENTER while having selected a row from a JTable? Have you wondered how to prevent the row selection from proceeding to the next row when this happens?

By default, the LAF for JTable installs an InputMap which translates keystrokes into action keys. If a keystroke's key has a corresponding key in the component's ActionMap, the Action for that key will be invoked. The Basic LAF installs a mapping for the ENTER key to the action key "selectNextRowCell", which is what changes the selection.

Without knowing this, you might simply add a key listener to the table:

This might work, but you may or may not still get the original response to ENTER depending on the order the key listeners are invoked. If another listener is called first, *and* it consumes the event, your handler might not even be called!

The proper solution is to remap the ENTER key to your own custom action:

    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!