开发者

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

开发者 https://www.devze.com 2023-03-03 23:38 出处:网络
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 an

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 i开发者_如何转开发s 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?


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


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);


Usually I would prefer to implement a new class that is inherited from DefaultComboBoxModel (therefore it's also a ListModel as well as a ComboBoxModel). This new class would be enriched with methods to update the model as any possible situation demands. In the update methods you would call fireContentsChanged to tell the enclosing component that the contents have changed and the component should redraw everything.

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消