如何从 ArrayList 填充 JComboBox? [英] How to populate a JComboBox from an ArrayList?

查看:33
本文介绍了如何从 ArrayList 填充 JComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象的 ArrayList,所有的对象都有名字.如何使用这些名称填充 JComboBox?我在网上查了一下,到目前为止一无所获.有一些资源,但它们倾向于使用硬编码版本,这是无用的.

I've got an ArrayList of objects, and all the objects have names. How can I populate a JComboBox with those names? I've looked online and found nothing so far. There are some resources, but they tend to go with a hardcoded version, which is useless.

由于我还没有任何代码显示我在做什么,所以我无法附加任何代码.

Since I don't have any code yet showing what I'm doing, I can't attach any.

推荐答案

JComboBoxArrayVector 一起使用.您应该使用 ArrayList 中的 .toArray() 方法来创建数组.

JComboBox works with Array or Vector. You should use .toArray() method in the ArrayList to create an array.

String[] names = namesList.toArray(new String[0]);

然后使用名称

这篇关于如何从 ArrayList 填充 JComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆