简体   繁体   English

ActionListener在模式JDialog中不起作用

[英]ActionListeners don't work in modal JDialog

I wrote my own class for modal dialog, but when I call it from my code there is no any reaction on buttons clicking. 我为模态对话框编写了自己的类,但是当我从代码中调用它时,单击按钮没有任何反应。 If I define setModal(false) everything works great. 如果我定义setModal(false)一切正常。 I suppose there is some troubles with concurrency, but I'm not sure about it. 我想并发有一些麻烦,但是我不确定。 Where is my mistake? 我的错误在哪里?

public class PauseTaskDialog extends JDialog {

private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JCheckBox prioritisingCheckBox;
private JCheckBox simultaneousWorkCheckBox;
private JCheckBox problemsWithDataCheckBox;
private JTextArea comment;

private String taskID;

public PauseTaskDialog(String task) {

    this.setContentPane(contentPane);
    this.setModal(true);
    this.setLocationRelativeTo(null);
    this.pack();

    this.setTitle("Task pause reasons");

    this.taskID = task;

    comment.setFont(comment.getFont().deriveFont(14f));
    comment.setLineWrap(true);
    comment.setWrapStyleWord(true);

    buttonOK.addActionListener(e -> {
        onOK();
    });

    buttonCancel.addActionListener(e -> {
        onCancel();
    });

    this.setVisible(true);
}


private void onOK() {
    // some code here        
}

private void onCancel() {
    // some code there
}
}

I call the dialog from my code this way: 我以这种方式从代码中调用对话框:

PauseTaskDialog dialog = new PauseTaskDialog(taskID);

From the docs : 文档

Note: changing modality of the visible dialog may have no effect until it is hidden and then shown again. 注意:更改可见对话框的模式可能不会生效,直到将其隐藏然后再次显示。

Try calling setModal(true) before setVisible . 尝试在setVisible之前调用setModal(true)

But setModal is deprecated, you should call setModalityType instead (the type you need is likely APPLICATION_MODAL ), check this tutorial . setModal已过时,你应该叫setModalityType代替(你需要的类型可能APPLICATION_MODAL ),检查此教程 it has nothing to do with JButton listeners not working, if you can CLICK on JButton then it means you are running their listeners(if there are any),if you cant click them(JButton has animation that shows that they are being clicked) then they are hidden/not on front, it has nothing to do with concurrency. 这与JButton侦听器不起作用无关,如果您可以单击JButton则表示您正在运行其侦听器(如果有),如果无法单击它们(JButton的动画显示它们正在被单击),则它们是隐藏的/不在前面,与并发无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM