java 使用 netbeans 将 ArrayList 加载到 JCombobox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15333941/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:16:33  来源:igfitidea点击:

Loading an ArrayList into a JCombobox using netbeans

javaswingnetbeansarraylistjcombobox

提问by Geuni

Currently have an ArrayList called SundayListwhich is loaded as soon as the frame AddStudentis loaded (bit of GUI)

当前有一个名为SundayList的 ArrayList ,它会在加载框架AddStudent 后立即加载(GUI 的位)

The code automatically generated by Netbeans is:

Netbeans自动生成的代码是:

comboboxSunday = new javax.swing.JComboBox();

comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item1", "Item2" }));

How do I load the combobox items with my own array? The array includes items such as:

如何使用我自己的数组加载组合框项目?该数组包括以下项目:

Activity1
Activity2
Activity3
Activity4

From my previous search, people mentioned about using a toString()and toArray(), and I'm not familiar with either methods, and how they help in loading the list into the combobox as I'm quite new to java..

从我之前的搜索中,人们提到了使用 atoString()toArray(),而我对这两种方法都不熟悉,以及它们如何帮助将列表加载到组合框中,因为我对 java..

回答by MadProgrammer

You could create your own ComboBoxModelthat takes a Listas the main parameter, but that's a little more involved

您可以创建自己的ComboBoxModel以 aList作为主要参数的方法,但这有点复杂

comboboxSunday.setModel(new DefaultComboBoxModel());
for (Object item : listOfItems) {
    comboboxSunday.addItem(item);
}

回答by barnacle.m

Assuming your array looks something like this:

假设你的数组看起来像这样:

String[] SundayList = { "Activity1", "Activity2", "Activity3", "Activity4" };

You can do this:

你可以这样做:

javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList);

If your array isn't a string array. try:

如果您的数组不是字符串数组。尝试:

javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList.toString());

Hope this helps!

希望这可以帮助!