Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to deselect a selected row in a JTable ?

 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

in my JTable (contains an own TableModel + TableCellRenderer) selection of rows is activated in a single row selection mode.

Now how do I deselect an already selected row ?
I would like to have it deselected, when clicking on it after having been selected.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code
1) Add a mouse listener
2) On mouse clicked, figure out over which row was the mouse clicked
3) Toggle the selection state

JTable#addRowSelectionInterval() and JTable#removeRowSelectionInterval() are your friends
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, that's it!
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... actually there's a very simple alternative to this:
just override method changeSelection in your subclass of JTable and set the toggle argument to true like this :


That's all. No mouse-listener and other code-stuff .
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The normal method for toggling the selection of a row is Ctrl+Click (on a PC, anyway - not sure about Mac). Won't this work? As a GUI user, this is what I would expect to work.
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark E Hansen wrote:Won't this work?


You're right, that works , I didn't know that before (I'm rather used to web applications)!
Thanks for the hint. I hope this works for UNIX and Mac platforms as well, but I think there will be other workarounds for this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic