• 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
  • 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

how to deselect a selected row in a JTable ?

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, that's it!
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic