使用特定目录的文件名填充 JComboBox

发布于 2024-10-19 20:36:28 字数 68 浏览 10 评论 0原文

我想用目录的文件名填充 JComboBox。然后,如果选择,每个字段都必须显示一个 JList。我怎样才能实现这个? 谢谢

I want to populate a JComboBox with file names of a directory. Then, if selected, each field has to show a JList. How can i implement this?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-10-26 20:36:28

您可以使用文件>>listFiles()

http://download.oracle。 com/javase/1.4.2/docs/api/java/io/File.html

获取特定目录(初始化 File 对象的目录)中的文件数组。

然后,您可以使用单个 File 的 getName() 方法来获取名称,然后使用 JComboBox 的 addItem() 方法添加这些名称:

http://download.oracle。 com/javase/1.4.2/docs/api/javax/swing/JComboBox.html

最后,要在用户单击这些名称之一时执行某些操作,您必须使用 JComboBox 的 addItemListener() 方法安装项目侦听器。有关于如何执行最后一部分的教程,通常它只是调用您的 ItemListener,为其提供一个 ItemEvent,然后您可以使用它来检查单击了哪个名称。

You can use File>>listFiles()

http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html

to get the array of Files in a particular directory (the one you initialized the File-object with).

You can then use the individual File's getName() method to get the names, then use JComboBox's addItem() method to add those names:

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JComboBox.html

Finally, to do something when the user clicks one of those names you have to install an item-listener using the JComboBox's addItemListener()-method. There are tutorials on how to do this last part and in general it just calls your ItemListener, giving it an ItemEvent, which you can then use to check which name was clicked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文