当我在 statechange 上显示对话框时,JCheckbox 会更改状态两次,如何修复?
我有一个复选框,当用户选择它时,应该生成一个包含更多信息的对话框,并根据用户的反应执行某些操作。我的代码基本上如下所示:
private void onItemStateChanged(java.awt.event.ItemEvent evt) {
System.out.println("STATE CHANGED!");//TODO debug code
if (evt.getStateChange() == ItemEvent.SELECTED) {
int returnVal = JOptionPane.showConfirmDialog(this, "blablatext");
if (returnVal == JOptionPane.OK_OPTION) {
this.field1TF.setText("");
this.field1TF.setEditable(false);
this.field2TF.setText("");
this.filed2TF.setEditable(false);
}else if(returnVal == JOptionPane.NO_OPTION){
this.field1TF.setText("");
this.field1TF.setEditable(false);
this.field2TF.setText("");
this.field2TF.setEditable(false);
}
} else if(evt.getStateChange() == ItemEvent.DESELECTED){
this.field1TF.setEditable(true);
this.field2TF.setEditable(true);
}
}
我现在的问题是,当我单击它时,我的复选框总是更改状态两次。 它在某种程度上与 JOptionPane.showConfirmDialog 有关,因为如果我将其注释掉,它就会按预期工作。 我是否没有意识到我应该关心的一些简单的事情,或者我必须做什么才能得到我想要的反应? (用户单击复选框 -> 被问到一个问题 -> 选择是/否/取消 -> 程序相应地执行操作)
I have a checkbox that, when the user selects it, should spawn a dialog with further info, and upon reaction from the user, do something. My code looks basically like this:
private void onItemStateChanged(java.awt.event.ItemEvent evt) {
System.out.println("STATE CHANGED!");//TODO debug code
if (evt.getStateChange() == ItemEvent.SELECTED) {
int returnVal = JOptionPane.showConfirmDialog(this, "blablatext");
if (returnVal == JOptionPane.OK_OPTION) {
this.field1TF.setText("");
this.field1TF.setEditable(false);
this.field2TF.setText("");
this.filed2TF.setEditable(false);
}else if(returnVal == JOptionPane.NO_OPTION){
this.field1TF.setText("");
this.field1TF.setEditable(false);
this.field2TF.setText("");
this.field2TF.setEditable(false);
}
} else if(evt.getStateChange() == ItemEvent.DESELECTED){
this.field1TF.setEditable(true);
this.field2TF.setEditable(true);
}
}
My problem now is, that my checkbox changes state always twice when I click on it.
It somehow has to do with the JOptionPane.showConfirmDialog because if I comment that out, it works as intended.
Am I not aware of something simple I should care about here, or what do I have to do to get my desired reaction? (User clicks checkbox -> is asked a question -> chooses YES/NO/Cancel -> program acts accordingly)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其更改为 ActionListener
JCheckBox http://download.oracle.com/javase/tutorial/ uiswing/components/button.html#checkbox
ActionListener http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
change that to the ActionListener
JCheckBox http://download.oracle.com/javase/tutorial/uiswing/components/button.html#checkbox
ActionListener http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
eeerggrrh,你必须看看 ItemListener,并使用这个演示....
eeerggrrhh, you have to look at ItemListener, and by using this demo ....
添加 ItemListener 并检查 isSelected 对我来说是这样:
Adding an ItemListener and checking for isSelected did it for me :