Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to restrict user from selecting a particular value in JLISt

 
ivin jacob
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JList bu the name organisationList and it contails many values
like ALL , US , UK, INDIA



The list allows multiple selection
I have a requirement in which the user should not be able to select ALL,
if he/she has already selected any other item

Can any body help me tackle this.

Regards,

Ivin.Jacob
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ivin jacob:


The list allows multiple selection
I have a requirement in which the user should not be able to select ALL,
if he/she has already selected any other item


Take a look at the API documentation. The JList has a setSelectionMode(int mode) method which will help you achieve the desired control.
 
ivin jacob
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm that doesnt help
 
ivin jacob
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that doesnt help
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:
The JList has a setSelectionMode(int mode) method which will help you achieve the desired control.


Perhaps he means setSelectionModel(ListSelectionModel selectionModel)? Unlike setSelectionMode(), that will indeed give you the control you need.

You will probably want to extend DefaultListSelectionModel, override some methods [setSelectionInterval(), addSelectionInterval(), possibly others] and set your custom selection model onto your JList.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:


Perhaps he means setSelectionModel(ListSelectionModel selectionModel)? Unlike setSelectionMode(), that will indeed give you the control you need.



No. I did mean setSelectionMode. and not setSelectionModel.
I have a requirement in which the user should not be able to select ALL,
if he/she has already selected any other item


If the mode is set to SINGLE_SELECTION, the user will not be able to select all. Is this the desired control or have I perhaps misunderstood the requirement?
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You misunderstood.

The selection should be either ALL, or any combination of INDIA, US and UK. So:
- if ALL is selected that should be the only selection
- if INDIA, US or UK is selected, any other can be selected except ALL

So the second case does allow for multiple selection.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
You misunderstood.



Oh I see it. I took ALL for all elements in the list. Doh!
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
The selection should be either ALL, or any combination of INDIA, US and UK. So:
- if ALL is selected that should be the only selection
- if INDIA, US or UK is selected, any other can be selected except ALL

So the second case does allow for multiple selection.


Here's a quick-and-dirty selection model that almost does this, but differs when the user tries to add ALL to a non-empty selection. Instead of preventing ALL from being selected, this code selects the ALL but deselects everything else.



As you can see, a custom selection model can pretty much control everything, which is why I thought perhaps Mr. Godbole intended to recommend setSelectionModel() instead of setSelectionMode().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic