如何使用 Java 中的数据加载已经实例化的 JComboBox?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2095686/
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
How to load already instantiated JComboBox with data in Java?
提问by Yatendra Goel
I have a Swings GUI which contains a JComboBox and I want to load data into it from the database.
我有一个包含 JComboBox 的 Swings GUI,我想将数据从数据库加载到其中。
I have retrieved the data from the database in a String Array. Now how can I populate this String array into the JComboBox
我已经从字符串数组中的数据库中检索了数据。现在如何将此 String 数组填充到 JComboBox 中
EDITED====================================================================
编辑================================================== ====================
In actual, the JComboBox is already instantiated when the java GUI is shown to the user. So I can't pass the Array as a paramter to the constructor.
实际上,在向用户显示 Java GUI 时,JComboBox 已经实例化。所以我不能将 Array 作为参数传递给构造函数。
How can I populate the already instantiated JComboBox?
如何填充已经实例化的 JComboBox?
The following is the code that is Nebeans generated code.
以下是Nebeans生成的代码。
jComboBox15 = new javax.swing.JComboBox();
jComboBox15.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "12" }));
jComboBox15.setName("jComboBox15");
Can I set another ComboBoxModel to the above jComboBox?
我可以将另一个 ComboBoxModel 设置为上述 jComboBox 吗?
采纳答案by OscarRyz
Here's an excellent article about it: How to use Combo Boxes ( The Java Tutorial )
这是一篇关于它的优秀文章:如何使用组合框(Java 教程)
Basically:
基本上:
String[] dbData = dateFromDb();
JComboBox dbCombo = new JComboBox(dbData);
You'll need to know other things like
你需要知道其他事情,比如
- Using an Uneditable Combo Box
- Handling Events on a Combo Box
- Using an Editable Combo Box
- Providing a Custom Renderer
- The Combo Box API
- Examples that Use Combo Boxes
- 使用不可编辑的组合框
- 处理组合框上的事件
- 使用可编辑的组合框
- 提供自定义渲染器
- 组合框 API
- 使用组合框的示例
That article contains information about it.
那篇文章包含有关它的信息。
EDIT
编辑
Yeap, you can either do what you show in your edited post, or keep a reference to the combo model:
是的,您可以按照您在编辑后的帖子中显示的内容进行操作,也可以保留对组合模型的引用:
DefaultComboBoxModel dcm = new DefaultComboBoxModel();
combo.setModel( dcm );
....
for( String newRow : dataFetched ) {
dcm.addElement( newRow )
}
回答by Bozho
new JComboBox(stringArray);
A useful tip - when you know what class you are working with, check its javadoc. It most often contains the information you need.
一个有用的提示 - 当你知道你正在使用什么类时,检查它的 javadoc。它通常包含您需要的信息。
Edit: after your update, use:
编辑:更新后,使用:
for (String string : stringArray) {
comboBox.addItem(string);
}
(my tip still applies)
(我的提示仍然适用)
回答by Jason Nichols
Ah, the combo box is already instantiated.... In that case just clear the contents and add the new array item by item:
啊,组合框已经实例化了......在这种情况下,只需清除内容并逐项添加新数组:
comboBox.removeAllItems();
for(String str : strArray) {
comboBox.addItem(str);
}
Make sure this is done from the EDT!
确保这是从 EDT 完成的!
回答by Montecarlo
I think that what NetBeans does is what you need.
我认为 NetBeans 所做的正是您所需要的。
From wherever you want, you can create a DefaultComboBoxModel
object and then invoke comboBox.setModel(defaultComboBox);
从你想要的任何地方,你可以创建一个DefaultComboBoxModel
对象,然后调用comboBox.setModel(defaultComboBox);
Here is a very small example of what I think you want to do: when the user clicks the button "Change data" the comboBox is filled with data from an array (method actionPerformed
).
这是我认为您想要做的一个非常小的示例:当用户单击“更改数据”按钮时,组合框将填充来自数组(方法actionPerformed
)的数据。
public class TestJComboBox extends JFrame {
private JComboBox comboBox = new JComboBox();
public TestJComboBox() {
JButton changeComboBoxData = new JButton("Change data");
changeComboBoxData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultComboBoxModel cbm = new DefaultComboBoxModel(
new String[] { "hola", "adios" });
comboBox.setModel(cbm);
}
});
super.setLayout(new BorderLayout(10, 10));
super.setSize(100, 100);
super.add(changeComboBoxData, BorderLayout.NORTH);
super.add(comboBox, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new TestJComboBox().setVisible(true);
}
}
回答by Cristian
JComboBox jComboOperator = new JComboBox();
arrOperatorName = new String []{"Visa", "MasterCard", "American Express"};
jComboOperator.setModel(new DefaultComboBoxModel(arrOperatorName));