• Post Reply Bookmark Topic Watch Topic
  • New 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

JTable - selecting a row after a right click from the mouse

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I select a row from a JTable when I right-click on it?
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a user right-clicks you know the position of the right-click (e.getX(), e.getY()).
int row = e.getY()/table.rowHeight;
The selected row can be found by the above formula. There is also one more way to find the selected row. You can use rowAtPoint() method in JTable to find the selected row.
After doing this, you can set the selected row by
doing
table.setRowSelectionInterval(row,row);
Here table is an instance of JTable.
Hope it helps. If you have any problems here lemme know.
Thanks,
- Raja.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, can this be done in a JList?
Happy middling with java.
Netharam.
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even JList has setSelectedIndex() and setSelectionInterval() methods. Refer to API documentation on JList for more explanation.
- Raja.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic