• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Greenhorn
Posts: 7
Hibernate Netbeans IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hernan Acosta
Greenhorn
Posts: 7
Hibernate Netbeans IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hernan Acosta
Greenhorn
Posts: 7
Hibernate Netbeans IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
Hibernate Netbeans IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
Hibernate Netbeans IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
Hibernate Netbeans IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic