This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disable Selection in a JTable

 
Oliver Brocker
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> But I want that the user cannot select any cell.

table.setEnabled(false);?
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Oliver Brocker
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Martin Bartlett
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullSelectionModel is very clean and works a treat here - thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic