java 更改 JComboBox 模型取消 ListModel。它有隐藏的后果吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5912823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing a JComboBox model unsing a ListModel. Does it have hidden consecuences?
提问by rciafardone
I use both JList and JComboBox in different places. The content of both change dynamically.
我在不同的地方同时使用 JList 和 JComboBox。两者的内容都是动态变化的。
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.
一旦创建了组合框,您就不能只说 comboBox.setModel(String[]),您必须创建一个新模型,然后将其设置为组合框。
Same happens with the JList.
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.
而不是创建我自己的 Jlist 和 ComboBox 只是为了添加一个名为 .setNewModel(String[]) 的新方法,我在我的“实用程序”类中创建了一个静态方法,它接收一个 String[] 并返回一个 ListModel。
So i can do this:
所以我可以这样做:
someComboBox.setModel((ComboBoxModel)UtilityClass.convetToListModel(aStringArray));
I use the same for the JList.
我对 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?
将 listModel 转换为 ComboBoxModel 会产生一些意想不到的后果吗?如果是这样,无论如何要更改组合框的整个内容而不必将 ArrayString 转换为模型?
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?
程序编译并运行良好,但转换总是让我产生怀疑,特别是复杂的对象。是的,我知道我可以扩展 JComboBox 和 JList 来添加一个方法来完成这项工作,但它有很多额外的工作。为什么 ComboBox 和 Jlist 没有更新或修改模型而不是接受简单的字符串数组?
回答by kleopatra
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
您添加的只是Utility 方法形式的白噪声。加
- the implementation of that method is simply ... crazy: you create a JComboBoxjust 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 ...
- 该方法的实现简直是......疯狂:你创建一个JComboBox只是为了访问由该组合内部创建的模型......
- 您必须利用实现来进行类型转换,以便在真正的组合中使用...
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
不要做这种浪费/不必要的事情,当有一个简单直接的方法可以达到同样的目标时,甚至不要想走任何弯路
回答by wolfcastle
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.
只需创建您自己的DefaultListModel
或DefaultComboBoxModel
将其传递到 JList/JComboBox。然后根据需要使用模型的添加/删除方法在内容发生变化时更新内容。
private DefaultComboBoxModel model = new DefaultComboBoxModel();
private JComboBox combo = new JComboBox(model);
...
model.addElement(somethingForMyList);
...
model.removeAllElements();
...
model.removeElement(elementToRemove);
回答by Osiris76
Usually I would prefer to implement a new class that is inherited from DefaultComboBoxModel(therefore it's also a ListModelas 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 fireContentsChangedto tell the enclosing component that the contents have changed and the component should redraw everything.
通常我更愿意实现一个从DefaultComboBoxModel继承的新类(因此它也是一个ListModel和一个ComboBoxModel)。这个新类将丰富方法以根据任何可能的情况需要更新模型。在更新方法中,您将调用fireContentsChanged来告诉封闭组件内容已更改并且组件应重绘所有内容。
Hope it helps.
希望能帮助到你。