Changing a JComboBox model unsing a ListModel. Does it have hidden consecuences?

15,642

Solution 1

How is

someComboBox.setModel((ComboBoxModel)UtilityClass.convetToListModel(aStringArray));

in any way easier to write/simpler/whatever than

someComboBox.setModel(new DefaultComboBoxModel(aStringArray))

all you added is white noise in the form of the Utility method. Plus

  • the implementation of that method is simply ... crazy: you create a JComboBox just for the sake of accessing the model that's internally created by that combo ...
  • you have to exploit implementation to type-cast for usage in a real combo ...

Don't do such wasteful/unnecessary stuff, don't even think of going any detours when there's a simple straightforward manner to reach the same goal

Solution 2

If the contents of the list/combobox need to change dynamically, then you should manage the model itself directly. You shouldn't create a new model each time and replace the old one. The whole point of having a model is that you can update the data it contains.

Simply create your own DefaultListModel or DefaultComboBoxModel and pass it into the JList/JComboBox. Then use the model's add/remove methods as needed to update the contents when it changes.

private DefaultComboBoxModel model = new DefaultComboBoxModel();
private JComboBox combo = new JComboBox(model);
...

model.addElement(somethingForMyList);
...
model.removeAllElements();
...
model.removeElement(elementToRemove);
Share:
15,642
rciafardone
Author by

rciafardone

Furry italian that loves to read and play video games. I am computer scientist first and a programmer second. I know some C, C++, C#, Java, Pascal, Visual Basic and SQL. I do not like Basic. I love C and his grandchildren (syntax wise) like Java and C#. Allman indentation Rules!!! BSD KNF style is the Devil, The Devil i say!!!

Updated on June 05, 2022

Comments

  • rciafardone
    rciafardone almost 2 years

    I use both JList and JComboBox in different places. The content of both change dynamically.

    Once a comboBox is created you cant just say comboBox.setModel(String[]), you have to create a new model and then set it to the comboBox.

    Same happens with the JList.

    Rather than creating my own Jlist and ComboBox just to add a new method called .setNewModel(String[]) i created a static method in my "utility" class that receives a String[] and returns a ListModel.

    So i can do this:

    someComboBox.setModel((ComboBoxModel)UtilityClass.convetToListModel(aStringArray));
    

    I use the same for the JList.

    someList.setModel(UtilityClass.convetToListModel(anotherStringArray));
    

    my question is:

    Could the casting of the listModel as a ComboBoxModel have some unexpected consequences? If so, is there anyway to change the entire content of a comboBox without having to transform the ArrayString into a Model?

    here is the code of the method:

    public static ListModel convertToListModel(String[] nList)
    {
        return (new JComboBox(nList).getModel());
    }
    

    The program compiles and runs fine, but casting always generates doubts in me, specially complex objects. Yes i know i can extend JComboBox and JList to add a method that does the job but its a lot of extra work. Why the ComboBox and Jlist don't have a update or modify Model than accept a simple array of Strings?