Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

how to deselect a selected row in a JTable ?  RSS feed

 
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.
 
Bartender
Posts: 11445
18
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 .
 
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
Boost this thread!