I am trying to create a program using MVC design pattern. In my model I have a created a Set of Strings. The Strings are just Set of about 45 locales.
In my view I am creating a JComboBox. I want to use the Set of the 45 unique locales in the order that they are in so I am using a LinkedHashSet.
I am having trouble trying to pass the locales to the JComboBox and there are a few things I'm not sure of.
EDIT: The main thing I would like to know is how to get my Set into my JComboBox. Below are a few other questions I thought of while writing this. Feel free to answer them if you wish!
- Should I be using a Set, is it the right collection.
- Should I be using a LinkedHashSet, is it the right implementation.
- Am I using a combobox correctly?
- Any advice etc.?
- I don't know much about data structures, any good resources for it?
Here's the code I'm using already:
View
private JComboBox<String> m_selectLocale = new JComboBox(getLocales());
LinkedHashSet<String> getLocales(){
System.out.println("running");
Set<String> localesSet = m_model.getLocales();
LinkedHashSet<String> locales = new LinkedHashSet<>(localesSet);
return locales;
}
Model
private static Set<String> localeSet = new LinkedHashSet<String>(Arrays.asList("All", "ar-ae", "ar-sa", "cs-cz", "da-dk", "de-at", "de-ch", "de-de", "el-gr", "en-ae", "en-au"));
public Set<String>getLocales(){
return localeSet;
}
I believe you can do something like this:
(In other words, create a new Vector by passing your ordered set to the 'add from a collection' constructor of Vector, and then use that to create your
ComboBoxModel
)UPDATE: looking at the Javadoc, looks like you can skip a step:
which, I believe, implicitly create a new ComboBoxModel for you.
FWIW, I think an OrderedSet (like LinkedHashSet) is a fine choice if you want to preserve insertion order of the choices. If you want to have, say, an alphabetical list of choices in your ComboBox, you may want to use an OrderedSet that uses the set members natural ordering, like TreeSet.