SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
albert kao wrote:Is there a way to use the java sort utility to sort all the values in the JComboBox?
luck, db
There are no new questions, but there may be new answers.
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.
albert kao wrote:Does it means I have to use Vector instead of Map?
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...)
Rob Camick wrote:Create a custom object to store the key/value data:
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.
The data is retrieved from database in both English and French language and stored in the Map<String, String> map
The user can click a button to switch the display between English and French language
Data is stored in two maps (key, value).
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