aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Change & sort the contents of JComboBox in Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Change & sort the contents of JComboBox in Swing" Watch "Change & sort the contents of JComboBox in Swing" New topic
Author

Change & sort the contents of JComboBox in Swing

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
My test program has a JComboBox which use the Swing DefaultComboBoxModel. Its values can be added by clicking a button.
How to sort the contents in the JComboBox?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Why not add it at the correct position immediately:
Of course this code sees C1000 come before C2; you may want to use a Comparator<String,String> instead of direct comparison.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
I modify my test program as per your suggestion.
Is there a way to use the java sort utility to sort all the values in the JComboBox?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

albert kao wrote:Is there a way to use the java sort utility to sort all the values in the JComboBox?


You seem to have missed the significance of the fact that the JComboBox has a model. The model is where the data is stored and the JComboBox is the thing which displays the data.

Therefore if you need to sort the data, you sort the values in the model. Asking how to sort the values in the JComboBox is the wrong question.
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
How to create a comboBox class in Swing so that its contents can be changed and can be sorted?
I am thinking of the following approach.
Will it work?
Is there any sample code on the internet which I should look at?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Looks good overall, just two points --

1. I feel SortedComboBoxModel or SortableComboBoxModel might be better names for the class, but that's just a personal opinion.

2. Don't forget to call fireContentsChanged(...) after sorting.


luck, db
There are no new questions, but there may be new answers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Just one comment: how do you plan to implement the addElement(Object) method when your underlying data is in a Map?

You do plan for users to add their own elements, right? After all that's the main reason why you would use a JComboBox and not a JList.
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
Paul Clapham wrote:Just one comment: how do you plan to implement the addElement(Object) method when your underlying data is in a Map?

You do plan for users to add their own elements, right? After all that's the main reason why you would use a JComboBox and not a JList.


Does it means I have to use Vector instead of Map?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Here is my version of a Sorted Combo Box Model
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

albert kao wrote:Does it means I have to use Vector instead of Map?


What I'm saying is that the natural collection underlying a JComboBox is a List, or an array. That's what DefaultComboBoxModel has the constructors which it does.

But you are choosing to have a Map as the underlying collection. I assume there was some design reason behind that -- you didn't say anything about that, you just posted code which used a Map in a trivial way. So your design has a Map. And your design displays the Map to the user via a JComboBox. That means that the user can add new entries to the Map, because that's one of a JComboBox's features. So there's a conflict between those two design choices, which you need to fix.

There are at least three ways to fix the conflict:

(1) Use a List instead of a Map.

(2) Use a JList instead of a JComboBox.

(3) Extend your design to figure out how to add an entry to your Map given a value without a key.

Since I know nothing about your design, I can't suggest which of these you should choose. Perhaps there are even other ways to fix the conflict which I haven't thought of. So it's up to you. Basically it looks like you didn't think the design through before you started programming and now you have to backtrack. (Which isn't all that unusual, really...)
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
Paul Clapham wrote:
albert kao wrote:Does it means I have to use Vector instead of Map?


What I'm saying is that the natural collection underlying a JComboBox is a List, or an array. That's what DefaultComboBoxModel has the constructors which it does.

But you are choosing to have a Map as the underlying collection. I assume there was some design reason behind that -- you didn't say anything about that, you just posted code which used a Map in a trivial way. So your design has a Map. And your design displays the Map to the user via a JComboBox. That means that the user can add new entries to the Map, because that's one of a JComboBox's features. So there's a conflict between those two design choices, which you need to fix.

There are at least three ways to fix the conflict:

(1) Use a List instead of a Map.

(2) Use a JList instead of a JComboBox.

(3) Extend your design to figure out how to add an entry to your Map given a value without a key.

Since I know nothing about your design, I can't suggest which of these you should choose. Perhaps there are even other ways to fix the conflict which I haven't thought of. So it's up to you. Basically it looks like you didn't think the design through before you started programming and now you have to backtrack. (Which isn't all that unusual, really...)


My requirements are:
- The data is retrieved from database in both English and French language and stored in the Map<String, String> map
- Use a JComboBox to display the value (not the key) of map. The display is sorted by value (not sorted by key) of map
- The user can click a button to switch the display between English and French language
- When the user choose an item in the JComboBox, save the choice by key

That is why my initial design is to store the data as a map in DefaultComboBoxModel.
This may or may not be the best design.
Feel free to suggest a better design.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Create a custom object to store the key/value data:

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
Rob Camick wrote:Create a custom object to store the key/value data:



Questions:
1. The sort by value function should be added to sort the model (Vector), right?
2. The Item class has the toString() method to allow keyboard navigation, right?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
1. I gave you an example of a model that will initially sort and then keep the model in sorted order. You can either implement the Comparable interface in the Item class or create a custom Comparator.

2. What happens if you comment out the toString() method.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

albert kao wrote:My requirements are:
- The data is retrieved from database in both English and French language and stored in the Map<String, String> map
- Use a JComboBox to display the value (not the key) of map. The display is sorted by value (not sorted by key) of map
- The user can click a button to switch the display between English and French language
- When the user choose an item in the JComboBox, save the choice either by value or key

That is why my initial design is to store the data as a map in DefaultComboBoxModel.
This may or may not be the best design.
Feel free to suggest a better design.


So... the English data is the key and the French data is the value? Or vice versa? Or something else? It's hard to suggest an alternate design when you don't have a description of the original design.

I didn't see anything in there which required you to use JComboBox instead of JList. Can you explain why that choice is necessary?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
@Paul, I don't understand your suggestion that you need to use a JList over a JCombobox? Both components are designed to display a single piece of data. In either case the model and/or the renderer will need to be customized to handle the key/value relationship. The issue (to me) is how to handle the data, not which component should be used to display the data.

The data is retrieved from database in both English and French language and stored in the Map<String, String> map


Must admit I didn't pay much attention to that earlier comment.

However, I would still suggest my approach to create a custom object that stores key/value pairs is the approach you want.

In your case I would actually create two models. One with the key/english value and the other with the key/french value.

The user can click a button to switch the display between English and French language


Then all you would do is replace the model used by the combo box. ie.

comboBox.setModel(englishModel);

or

comboBox.setModel(frenchModel);

This approach does not require any customization to the model in terms of the way the data is stored (You do need to customize the model to keep the data sorted). With this approach you could also easily expand the languages you support.
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
Data is stored in two maps (key, value).
e.g.
English map
1, poor
2, satisfactory
3, good
4, excellent

French map
1, pauvres
2, satisfaisant
3, bonne
4, excellente

The JComboBox will display in alphabetical sorted order either
in English
excellent, good, poor, satisfactory
or in French
bonne, excellente, pauvres, satisfaisant

Please note that the requirement should be
"When the user choose an item in the JComboBox, save the choice by key".
e.g. if the user choose (3, good), then the key "3" should be saved.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Data is stored in two maps (key, value).


Well, that is what you are currently doing, but that is not the approach I suggested.

I don't see why you would need support for dynamically adding items to the model. So the best option is to just create your custom objects and add them to a Vector and then sort the Vector before creating the DefaultComboBoxModel.

I've given you working code on how this might be done. The only thing you need to do is implement the Comparable method on your custom Object.

Post your SSCCE if you have problems.
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
Rob Camick wrote:
Data is stored in two maps (key, value).


Well, that is what you are currently doing, but that is not the approach I suggested.

I don't see why you would need support for dynamically adding items to the model. So the best option is to just create your custom objects and add them to a Vector and then sort the Vector before creating the DefaultComboBoxModel.

I've given you working code on how this might be done. The only thing you need to do is implement the Comparable method on your custom Object.

Post your SSCCE if you have problems.


Data is stored in two maps (key, value) because the database API return the requested data in a map.
i.e. in pseudo code:
English map = db(choice, "English");
French map = db(choice, "French ");
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Data is stored in two maps (key, value) because the database API return the requested data in a map


That sounds like your custom API because normally database queries return a ResultSet.

My suggestion is to just copy the data from the map and create your custom Objects the way I explained earlier.
 
wood burning stoves
 
subject: Change & sort the contents of JComboBox in Swing