使用特定目录的文件名填充 JComboBox
我想用目录的文件名填充 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用文件>>listFiles()
获取特定目录(初始化 File 对象的目录)中的文件数组。
然后,您可以使用单个 File 的 getName() 方法来获取名称,然后使用 JComboBox 的 addItem() 方法添加这些名称:
最后,要在用户单击这些名称之一时执行某些操作,您必须使用 JComboBox 的 addItemListener() 方法安装项目侦听器。有关于如何执行最后一部分的教程,通常它只是调用您的 ItemListener,为其提供一个 ItemEvent,然后您可以使用它来检查单击了哪个名称。
You can use File>>listFiles()
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:
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.