Search code examples
javaswingjframejdialogcomponentlistener

Why are the Window/Component Listeners invoked differently when setVisible(false) and dispose() are called?


The difference I see is (running on JDK 1.7):

setVisible(false), invokes componentHidden but not windowClosed (The API states only on dispose() so it's OK even if it irritates me)

but

dispose(), invokes windowClosed but not componentHidden

Short running example code (MCVE):

public class JDialogTest extends JDialog {
    private static final long serialVersionUID = 1L;

    public JDialogTest(JFrame owner){
        super(owner,ModalityType.APPLICATION_MODAL);
        init();
    }
    private void init() {
        this.getContentPane().setLayout(new GridLayout(1,2));
        JButton btnVisible = new JButton("Set visible false");
        btnVisible.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialogTest.this.setVisible(false);
            }
        });
        JButton btnDispose = new JButton("Dispose");
        btnDispose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialogTest.this.dispose();
            }
        });
        this.getContentPane().add(btnVisible);
        this.getContentPane().add(btnDispose);
        this.pack();
    }

    public static void main(String[] args) {

        //A fake frame to test JDialog
        JFrame fakeFrame = new JFrame("Fake Frame");
        fakeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fakeFrame.getContentPane().setLayout(new BorderLayout());
        JButton btnOpen = new JButton("Open Dialog");
        fakeFrame.getContentPane().add(btnOpen,BorderLayout.CENTER);
        fakeFrame.pack();
        fakeFrame.setLocationRelativeTo(null);

        //Generate the test dialog
        final JDialogTest dialog = new JDialogTest(fakeFrame);
        dialog.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentShown(ComponentEvent e) {
                System.out.println("Component Shown");
            }
            @Override
            public void componentHidden(ComponentEvent e) {
                System.out.println("Component Hidden");
            }
        });

        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("Window open");
            }
            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("Window closed");
            }
        });
        dialog.setLocationRelativeTo(null);

        btnOpen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(true);
            }
        });
        fakeFrame.setVisible(true);
    }
}

NOTE: The example features a JDialog, but I see same behavior in JFrame, to test simple attach the listeners to the fakeFrame, and add similar buttons. (avoided in MVCE to keep it Minimal)).

I have considered this post:

JDialog setVisible(false) vs dispose()

  • In answers seems that is should be no difference, use dispose()...

API DOCUMENTATION:

Window.setVisible(boolean b), Window.dispose(), ComponentListener.componentHidden(ComponentEvent e), WindowListener.windowClosed(WindowEvent e)

Why do I care: Naturally out of curiosity, but also since I use buttons to close the window (invoking dispose()) and the interface can also be closed by the top/right window close icon and alt+F4 (invoking setVisible(false)!?). Hence no one of the above listener can be used. Only the HierarchyListener will catch them both and this seems counter intuitive.

EDIT: The question is scoped as "why is it like this"? What is the purpose?". I was expecting dispose() to invoke both! I can't find any clues in the API documentation for why not.


Solution

  • the interface can also be close by top/right window close icon alt+F4 (invoking setVisible(false)!?)

    This is determined by the default close operation. You can set it with setDefaultCloseOperation. The default is HIDE_ON_CLOSE, which is why you get a componentHidden invocation. If you set it to DISPOSE_ON_CLOSE then you will get a windowClosed invocation instead. Setting to the latter will allow you to only register for those event types.

    Regardless, hiding and disposing do different things. Disposing releases the resources used while hiding doesn't. Also, hiding the last window will not exit the JVM while disposing it will.

    As for the technical side of event dispatching, there are a lot of intricacies. While disposing the window does call its hiding method, the dispatching of events is done after operations are done. This means that the EDT can dispatch events "after the fact". Since the window is closed, it does not dispatch the hiding event.