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 restrict user from selecting a particular value in JLISt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to restrict user from selecting a particular value in JLISt" Watch "how to restrict user from selecting a particular value in JLISt" New topic
Author

how to restrict user from selecting a particular value in JLISt

ivin jacob
Greenhorn

Joined: Apr 05, 2006
Posts: 23
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
ivin jacob
Greenhorn

Joined: Apr 05, 2006
Posts: 23
hmm that doesnt help
ivin jacob
Greenhorn

Joined: Apr 05, 2006
Posts: 23
that doesnt help
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Sep 20, 2005
Posts: 862
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().
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to restrict user from selecting a particular value in JLISt