在 JComboBox 上添加 ListSelectionListener
我对 Java Swing 还很陌生。我一直试图在 JComboBox
实例上添加 ListSelectionListener
。似乎只有 ListSelectionModel
接口有 addListSelectionListener
方法。我有点想不通......
为什么我想添加它是因为我希望程序做一些事情,即使组合框中的项目在选择后没有改变。
潜在答案 我只是想在组合框上附加一个动作监听器不起作用。我认为这是openjdk的错误。我已在此处报告了此问题,
提前致谢。
I'm quite new to Java Swing. And I'm stuck on trying to add a ListSelectionListener
on a JComboBox
instance. It seems only the ListSelectionModel
interface has addListSelectionListener
method. I kind of cannot figure it out...
Why I want to do add it is that I want program do something even the item in the combo box is not changes after selecting.
POTENTIAL ANSWER
I was simply thinking of attaching an actionListener on combobox not working. and i think it's bug of openjdk. I've reported it here
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下 JComboBox#addItemListener:
鼠标和键盘交互都会触发此事件。
Take a look at JComboBox#addItemListener:
This event is fired for both mouse and keyboard interaction.
对于 JComboBox,您必须使用 ActionListener。
AFAIK,只要用户对 JComboBox 进行选择,即使它与已选择的项目相同,也会引发 actionPerformed。
For JComboBox, you'll have to use ActionListener.
AFAIK, actionPerformed is raised whenever the user makes a selection for the JComboBox even if it's the same item that was already selected.
这取决于您的要求。 ActionEvent 仅在使用键盘时触发,而不是当鼠标在项目上移动时选择发生变化时触发。
如果您想在项目选择更改时执行某些操作,即使鼠标移动,那么您可能需要访问 JList。您可以使用以下代码访问弹出窗口使用的 JList:
It depends on your requirement. The ActionEvent is only fired when the keyboard is used, not when the selection changes as the mouse is moved over the items.
If you want to do some action when the item selection changes even if the mouse is moved then yes you will probably need access to the JList. You can access the JList used by the popup with the following code:
使用 PopupMenuListener。当弹出菜单关闭时,获取所选索引并进行处理。
Use a PopupMenuListener. When the popup menu closes get the selected index and do your processing.