JOptionPane 显示在选定的 JCheckBox 上
大家好,我在 JcheckBox 侦听器中添加 joptionpane 时遇到一些困难,
public void itemStateChanged(ItemEvent evt) {
if(evt.getStateChange() == ItemEvent.SELECTED){
///some code
JOptionPane.showMessageDialog(null, "Message", "Alert",
JOptionPane.INFORMATION_MESSAGE);
}
}
因此它工作正常,但问题是 JCheckBox 被选中并立即取消选择,我该如何解决这个问题?
干杯
Hi all I am having some difficulties with adding a joptionpane in JcheckBox listener
public void itemStateChanged(ItemEvent evt) {
if(evt.getStateChange() == ItemEvent.SELECTED){
///some code
JOptionPane.showMessageDialog(null, "Message", "Alert",
JOptionPane.INFORMATION_MESSAGE);
}
}
so it works fine,but the problem is that the JCheckBox gets selected and immediately deselected how can I manage to fix this ?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些建议(解决方案)使用操作侦听器而不是项目侦听器。这确实有效,但是,我觉得很奇怪,因为我建议项目侦听器的所有文本都是复选框的预期侦听器类型。
事实上,这是 Oracle Bug ID:6924233 承认的已知错误JOptionPane 显然会导致生成另一个事件。
建议的修复方法是使用 invokeLater 调用 JOptionPane。这工作得很好,只需要对已经将项目侦听器用于其他目的的程序进行较小的代码更改。
There are a couple of suggestions here (solution) to use an action listener instead of a item listener. This does work, however, I though it strange given that all the texts I have suggest an item listener is the expected type of listener for a check box.
In fact, this is a known bug as acknowledged by Oracle Bug ID:6924233 The JOptionPane apparently causes another event to be generated.
The recommended fix is to invoke the JOptionPane using invokeLater. This works fine and involves only a minor code change to a program already using an item listener for other purposes.
问题一定出在“///some code”中,因为以下测试程序在 Java 6 中适用于我:
查看 setSelected 或 doClick 调用的省略代码。
The problem must be in "///some code" as the following test program works for me in Java 6:
Have a look in the omitted code for setSelected or doClick calls.