How to really close a JDialog with Java code?

南笙酒味 提交于 2019-11-28 00:24:29
trashgod

Send the JDialog a WindowEvent.WINDOW_CLOSING event using dispatchEvent(), as shown here and here.

Addendum: System.exit() should be used with care, as discussed here and here. In the example below, a second, non-daemon thread completes normally, even if the dialog is closed before its loop exits. See JLS §12.8 Program Exit for details.

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

/**
 * @see https://stackoverflow.com/questions/8336161
 * @see https://stackoverflow.com/questions/6163606
 */
public class DialogEventTest extends JDialog {

    public DialogEventTest() {
        this.setLayout(new GridLayout(0, 1));
        this.add(new JLabel("Dialog event test.", JLabel.CENTER));
        this.add(new JButton(new AbstractAction("Close") {

            @Override
            public void actionPerformed(ActionEvent e) {
                DialogEventTest.this.setVisible(false);
                DialogEventTest.this.dispatchEvent(new WindowEvent(
                    DialogEventTest.this, WindowEvent.WINDOW_CLOSING));
            }
        }));
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println(e.paramString());
            }
        });
    }

    private void display() {
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogEventTest().display();
            }
        });
        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("Starting…");
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                        System.out.println((i + 1) + "s. elapsed.");
                    } catch (InterruptedException e) {
                        e.printStackTrace(System.err);
                    }
                }
                System.out.println("Finished.");
            }
        }).start();
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!