File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes how to deselect a selected row in a JTable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to deselect a selected row in a JTable ?" Watch "how to deselect a selected row in a JTable ?" New topic
Author

how to deselect a selected row in a JTable ?

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
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.


SCJP, SCJD
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
thanks, that's it!
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
... 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

Joined: Apr 01, 2009
Posts: 642
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

Joined: Feb 07, 2010
Posts: 150
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to deselect a selected row in a JTable ?
 
Similar Threads
JTable
JTable
Jlist or Jtable?
JTable
Jtable, Jtextarea