java 填充 JComboBox 的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11186649/
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
The correct way to populate a JComboBox?
提问by London Student
I am currently building an application in Java on Eclipse as a self help guide to programming fundamentals and basic java programming, this is purely educational and for the sole purpose of being able to reference topics easily and practice my programming as I learn them by programming them into this tutorial application.
我目前正在 Eclipse 上用 Java 构建一个应用程序,作为编程基础和基本 Java 编程的自助指南,这纯粹是教育性的,其唯一目的是能够轻松地参考主题并在我通过编程学习它们时练习我的编程进入本教程应用程序。
The content of the application will expand as time goes on and as I learn more components of programming.
随着时间的推移和我学习更多的编程组件,应用程序的内容将会扩展。
So my first question comes down to correct form.
所以我的第一个问题归结为正确的形式。
I am using a drop down box (JComboBox
) so as to select specific topics from within the GUI. I would like to populate the list and keep the program clean and tidy. So my question is how would one populate the JComboBox
so as to limit cluttering code. Perhaps a text file from which I could add topics to separately and edit more efficiently? I am after correct programming procedure as opposed to all the ways I could do it. I know I could use an ArrayList
, however I am keen to understand the choices taken when using large amounts of content as opposed to very little.
我正在使用下拉框 ( JComboBox
) 以便从 GUI 中选择特定主题。我想填充列表并保持程序干净整洁。所以我的问题是如何填充JComboBox
以限制混乱的代码。也许是一个文本文件,我可以从中单独添加主题并更有效地进行编辑?我追求的是正确的编程过程,而不是我能做到的所有方式。我知道我可以使用ArrayList
,但是我很想了解在使用大量内容而不是很少内容时所采取的选择。
Thanks,
谢谢,
Simon
西蒙
采纳答案by Heisenbug
I think the cleanest way is to define a custom ComboBoxModel.
我认为最干净的方法是定义一个自定义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.
使用文本文件可能是一件好事,因为在插入新条目时您不必修改代码。您可以在 ComboBoxModel 构造函数中定义读取文件过程。这样每次运行程序时,您都会发现更新的组合框内容。
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.
如果应用程序本身无法更新内容,则 ArrayList 不是一个好的选择。如果您对数组列表的内容进行硬编码,则每次需要添加新条目时都将被迫修改代码。
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.
一旦编写了您需要的内容,您将不再修改代码。您也可以对多个不同的 JComboBox 使用相同的模型。
YourModel model = new YourModel("path_to_a_file");
JComboBox box1 = new JComboBox();
box1.setModel(model);
JComboBox box2 = new JComboBox();
box2.setModel(model);
回答by GETah
The easiest way of populating a Combobox is (as Java documentationstates) is:
填充组合框的最简单方法是(如Java 文档所述)是:
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...)
但是,这不是您可以选择的最佳选择。用字符串数组填充组合框并不是提供灵活性和模型/UI 解耦的最佳选择。这就是 MVC 模型发挥作用的地方。MVC 模型基本上告诉您使用模型(在您的情况下 a ComboBoxModel
)来支持您的数据。拥有模型为您提供了从您想要的任何地方(文件、套接字、Web 服务...)获取数据的可能性和灵活性
回答by Gandalf
An alternative way to using a custom ComboBoxModel would be to use JGoodies Bindingto 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:
使用自定义 ComboBoxModel 的另一种方法是使用JGoodies Binding将您的视图绑定到视图模型。这样做您的视图模型不包含任何特定于视图的代码,而是使用标准的 java bean 机制(例如属性更改支持)来按需更新视图,并且它通过 bean 属性自动接收所有视图更新。显示的数据实际来自何处(在示例中它直接来自 java 枚举)与视图实现无关。下面是一个例子:
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 {
//....
}