Question

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!

  1. Should I be using a Set, is it the right collection.
  2. Should I be using a LinkedHashSet, is it the right implementation.
  3. Am I using a combobox correctly?
  4. Any advice etc.?
  5. 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;
}
Was it helpful?

Solution

I believe you can do something like this:

m_selectLocale = new JComboBox(new DefaultComboBoxModel(new Vector<String>(getLocales())));

(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:

m_selectLocale = new JComboBox(new Vector<String>(getLocales()));

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.

OTHER TIPS

Should I be using a Set, is it the right collection.

This will depend on what it is you hope to achieve. Set will guarantee uniqueness. Based on the limited information, I would suggest this is not a bad choice.

Should I be using a LinkedHashSet, is it the right implementation.

Again, this will depend on what it is you want to achieve. Any linked structure is well suited for serialised access (moving from the start to the end), but not so good for random access (jumping into the list and arbitrary points)

This may make it a bad choice for using with a Combo box

Am I using a combobox correctly?

Given the fact that the code won't compile, probably not. JComboBox takes either a ComboBoxModel, array or Vector, not any type of Set.

Personally, I would create my I own combo box model, backed by the Set. This way you won't need to translate the Set to some other kind of structure and will save you on (some small amount of) memory.

Any advice etc.?

Read the tutorials

I don't know much about data structures, any good resources for it?

As per the last question

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top