• 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

Disable Selection in a JTable

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> But I want that the user cannot select any cell.

table.setEnabled(false);?
 
Maneesh Godbole
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

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NullSelectionModel is very clean and works a treat here - thanks
 
Destroy anything that stands in your way. Except this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic