The correct way to populate a JComboBox?

馋奶兔 提交于 2019-12-05 08:27:20
Heisenbug

I think the cleanest way is to define a custom ComboBoxModel.

This way you can define a data model for your combobox, separating the part in which the combobox is created from the data management itself.

Probably using a text file is a good thing, since you don't have to modify the code when a new entry is inserted. You can define the reading file procedure inside your ComboBoxModel constructor. This way every time you run the program you'll find updated combobox's contents.

ArrayList isn't a good choice if the contents can't be updated by the application itself. If you are hardcoding the contents of an arraylist, you will be forced to modify the code every time you need to add a new entry.

A little example:

class YourModel implements ComboBoxModel{

//implements all interface methods required...
@override
public YourModel(String filename)
{
    comboBoxItemList = new ArrayList<String>();
    // open your file
    // add every entry to the the list
}
@override
public Object getElementAt(int index)
{
    return comboBoxItemList.get(index);
}
List<String> comboBoxItemList;
}

Once written what you need you will not to modify the code anymore. And you can use the same model for several different JComboBox also.

YourModel model = new YourModel("path_to_a_file");
JComboBox box1 = new JComboBox();
box1.setModel(model);
JComboBox box2 = new JComboBox();
box2.setModel(model);

The easiest way of populating a Combobox is (as Java documentation states) is:

String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox petList = new JComboBox(petStrings);

This is, however, not the best option you could go for. Populating your combobox with an array of strings is not the best option that offers flexibility and model/UI decoupling. This is where the MVC model comes into play. The MVC model basically tells you to use a Model (in your case a ComboBoxModel) to back your data out. Having a model offers you the possibility and flexibility of getting your data from anywhere you want (files, sockets, web service...)

An alternative way to using a custom ComboBoxModel would be to use JGoodies Binding to bind your view to a view model. Doing this your view model does not contain any view specific code but uses standard java bean mechanisms (e.g. property change support) to update the view on demand and it receives all view updates automatically through bean properties. Where the data displayed actually comes from (in the example it comes directly from a java enum) is irrelevant for the view implementation. Here is an example:

class View {
    private JComboBox chatPresenceCombo = new JComboBox();

    public bind(ViewModel viewModel) {
        BeanAdapter<ViewModel> beanAdapter = new BeanAdapter<ViewModel>(viewModel, true);
        Bindings.bind(chatPresenceCombo, new SelectionInList<ChatPresence>(viewModel.getChatPresenceValues(),
                beanAdapter.getValueModel(ViewModel.PROPERTY_CHAT_PRESENCE)));
    }
}

class ViewModel
{
    private ChatPresence chatPresence;

    private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public static final String PROPERTY_CHAT_PRESENCE = "chatPresence";

    public ChatPresence getChatPresence() {
        return chatPresence;
    }

    public void setChatPresence(ChatPresence chatPresence) {
        ChatPresence old = this.chatPresence;
        this.chatPresence = chatPresence;
        changeSupport.firePropertyChange(PROPERTY_CHAT_PRESENCE, old, chatPresence);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

    public ChatPresence[] getChatPresenceValues() {
         return ChatPresence.values();
    }
}

public enum ChatPresence {
    //....
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!