wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to allow 'Select none' in a JComboBox or JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to allow Watch "How to allow New topic
Author

How to allow 'Select none' in a JComboBox or JList

Hernan Acosta
Greenhorn

Joined: Mar 15, 2011
Posts: 7

Hello,

I need to add an option 'Select none' in a JCombobox. It's a very common issue.

In code, I can use:



But, ¿how can I add that option to the jCmbBox, so the user can 'Select none', without adding that option to the jCmbBox model?.

I need this:

-None selected-
Chevrolet
Fiat
Ford
Mercedes Benz
Nissan
Peugeot
Toyota

Thanks in advance (and excuse me because of my english)


Quidquid latine dictum sit, altum viditur.
Edwin Torres
Ranch Hand

Joined: Mar 19, 2011
Posts: 37
What's wrong with adding a NONE option as your first item in the ComboBox? In the code, you can act accordingly when the user selects none.


Follow me: @CompGuyProbs
Read me: Freehold Computers Examiner
Hernan Acosta
Greenhorn

Joined: Mar 15, 2011
Posts: 7

Thanks Edwin for your answer.

I can't figure out how to do it. This is my code:



¿How can I add the NONE option?
If the user selects NONE, the selected value has to be NULL. I don't want to add that option to my model, because I don't want to complicate it.
Edwin Torres
Ranch Hand

Joined: Mar 19, 2011
Posts: 37
Hernan Acosta wrote:
I can't figure out how to do it. ...
¿How can I add the NONE option?
If the user selects NONE, the selected value has to be NULL. I don't want to add that option to my model, because I don't want to complicate it.


Just create your ComboBox this way:

The first element in your myPatients array should be something like "- none -". If it isn't, create a new array that has "- none -" as the first element, then fill in the rest of the elements with your myPatients array.

You don't have to call setSelectedIndex() when you are creating your ComboBox.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Another approach could be to place a JCheckBox to the left of the JComboBox that would both setSelectedIndex(-1) and disable the JComboBox when unchecked.
Whether you initially check the box and enable the combo, or uncheck the box and disable the combo, is up to you. You could also cache the last selected value in an instance field so that if the used accidentally unselects the checkbox, the selection can be restored by checking it again.


luck, db
There are no new questions, but there may be new answers.
Hernan Acosta
Greenhorn

Joined: Mar 15, 2011
Posts: 7

Edwin,
I can't figure out how to combine that string "None" with my Patients array(). Maybe concatenating in an Object[] array, but I don't know how.
I also tried using:


But doing this the element is added at the end of the list, instead of the beginning.

I use:
because I don't want any element being selected by default. If I don't use this line, the selected item is the first item of the model.
Hernan Acosta
Greenhorn

Joined: Mar 15, 2011
Posts: 7

Darryl, thank you for your response.

That approach is interesting. In some cases, in my application, it would be useful. But I have a large panel with many swing components. And the people using this application doesn't have much experience with computers, so I want to have a more intuitive UI.
Edwin Torres
Ranch Hand

Joined: Mar 19, 2011
Posts: 37
Hernan Acosta wrote:Edwin,
I can't figure out how to combine that string "None" with my Patients array(). Maybe concatenating in an Object[] array, but I don't know how.
I also tried using:


Sorry, but I didn't realize that your array had Patient objects. I thought you were using Strings. In any case, here's how you can combine a String to an existing String array:

You could do the same with your Patients array, but you'd have to come up with the notion of a "blank" Patient object for your first element. Sorry, if I confused the issue.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

But doing this the element is added at the end of the list, instead of the beginning.

Take another look at the API for DefaultComboBoxModel. There's a method that Adds an item at a specific index.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

If the user selects NONE, the selected value has to be NULL. I don't want to add that option to my model, because I don't want to complicate it.

You can sill have a model that contains a null (not NULL) and a custom renderer that displays an appropriate String like " --- None --- "
http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

If you choose to go that route, I would suggest extending DefaultListCellRenderer
Hernan Acosta
Greenhorn

Joined: Mar 15, 2011
Posts: 7

Edwin, Darryl, thank you very much for you time.

I will try it with a combination of your approaches (CombinedArrays + Renderer). It gave me more ideas to resolve it.

I thought too, that there's addElement(object, index) method, but, oddly, there isn't.

Thank you again for your responses.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Hernan Acosta wrote:I thought too, that there's addElement(object, index) method, but, oddly, there isn't.


Aw, c'mon! There are only 10 methods declared in DefaultComboListModel. How long can it possibly take to go through all of them (not that you'll need to, once you read the API).
Hernan Acosta
Greenhorn

Joined: Mar 15, 2011
Posts: 7

Oh, how embarrasing! I really looked into the API, I don't know what hapenned to my.


So, the simplest way to resolve my issue is doing:



Then, when the user selects an element, if the selected index == '0', I would set null value, else I would set the selected Patient object.

Thank you very much. I Appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to allow 'Select none' in a JComboBox or JList
 
Similar Threads
How Drop Down Box values can be retained
Jtable combo box issue
JTable +JComboBox
JComboBox Problem...Help
dynamically adding elements to another using javascript