Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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