Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
    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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • 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:


 
You can't have everything. Where would you put it?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!