wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Disable Selection 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 "Disable Selection in a JTable" Watch "Disable Selection in a JTable" New topic
Author

Disable Selection in a JTable

Oliver Brocker
Greenhorn

Joined: Jun 11, 2008
Posts: 15
On a javax.swing.JTable I can set the table's selection mode with setSelectionMode. Here I can set it to SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION or MULTIPLE_INTERVAL_SELECTION.

But I want that the user cannot select any cell. So is there a way to disable the selectionmode? Unfortunately there is no NONE_SELECTION alternative.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Originally posted by Oliver Brocker:
...But I want that the user cannot select any cell. So is there a way to disable the selectionmode? Unfortunately there is no NONE_SELECTION alternative.


You can do it in two ways:
1) Listen to row selections using a selection listener. On receiving the selection notifications, make a call to JTable#clearSelection
2) Provide renderers and tweak the selection background and selection foreground to be the same as the default background and foreground. Mind you in this case, any call to the table's selection model will return data as you are tweaking only the visual part and not touching the underlying selection model.


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

Joined: Jun 09, 2003
Posts: 4632
> But I want that the user cannot select any cell.

table.setEnabled(false);?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Originally posted by Michael Dunn:

table.setEnabled(false);?


I havent tried it, but wont this affect the sorting and poupus and column resizing too?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it'll affect just about everything, but the OP's exact requirements remain unknown.

as for affecting sorting, loosely that could infer column selection,
whether that is an expansion of cell selection, only the OP knows.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Oliver Brocker:
On a javax.swing.JTable I can set the table's selection mode with setSelectionMode. Here I can set it to SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION or MULTIPLE_INTERVAL_SELECTION.

But I want that the user cannot select any cell. So is there a way to disable the selectionmode? Unfortunately there is no NONE_SELECTION alternative.


yourTable.setSelectionModel( new NullSelectionModel() );

Note that cells may still be editable even though they can't be selected.
[ July 24, 2008: Message edited by: Brian Cole ]

bitguru blog
Oliver Brocker
Greenhorn

Joined: Jun 11, 2008
Posts: 15
Many thanks for all replies

I take Maneesh Godbole's #1 proposal and it works fine for me.
Maybe I will switch to another solution in the future. To set a NullSelectionModel is a nice idea. But first I try what happen with table.setEnabled(false) - I'm not sure if I already try it.
Suraj Chandran
Ranch Hand

Joined: May 20, 2008
Posts: 53
The right way to do is to use how Brian Cole said.
Use his NUllSelectionModel.
I think table.setEnabled() is not a fine idea. The problem is related to selection hence you should only fiddle with the selection model. Using table.setEnabled(false) will have so many side-effects...like editing is lost...events will be lsot...etc...


I guess there are couple of design issues in this classic software the GOD coded.
Martin Bartlett
Greenhorn

Joined: Dec 03, 2009
Posts: 1
NullSelectionModel is very clean and works a treat here - thanks
 
wood burning stoves
 
subject: Disable Selection in a JTable