Remove “X” button in Swing JDialog

前端 未结 6 874
时光说笑
时光说笑 2020-12-08 18:18

Is there a way to remove the close button (\"X\") from the JDialog title bar?

6条回答
  •  执笔经年
    2020-12-08 19:06

    Here is my experience:

    • Tried using setUndecorated(true): Made the whole Dialog invisible.
    • Tried setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE): This didn't change the behavior at all. My dialog box still closed. Setting the default close operation to DO_NOTHING_ON_CLOSE delegates the close operation to the windowClosing() method of a registered WindowListener.

    What worked for me was:

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    //Remove any existing WindowListeners
    for (WindowListener wl : this.getWindowListeners()) {
        this.removeWindowListener(wl);
    }
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if ("Optional condition") {
                JOptionPane.showMessageDialog(null, "You cannot close this window");
            }
        }
    });
    

提交回复
热议问题