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

The difference I see (works on JDK 1.7):

setVisible(false) calls componentHidden but not windowClosed (the API only declares to dispose() , so it's ok even if it annoys me)

but

dispose() calls windowClosed but not componentHidden

Short Run Code 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: There is JDialog in the example, but I see the same behavior in JFrame to check, just connect listeners to fakeFrame and add similar buttons. (avoided in MVCE to keep it M inimal)).

I reviewed this post:

JDialog setVisible (false) vs dispose ()

  • The answers seem to make no difference, use dispose() ...

API DOCUMENTATION:

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

Why is it important to me . Naturally, out of curiosity, but also because I use the buttons to close the window (calling dispose() ), and the interface can also be closed by closing the top / right window icon and alt + F4 (calling setVisible(false) !?). Therefore, none of the above listener can be used. Only the HierarchyListener will catch them both, and this seems like an intuitive counter.

EDIT: The question is considered as "why is this so"? What is the purpose? "I expected dispose() call both! I can't find any hints in the API documentation, why not.

+6
source share
1 answer

the interface may also be close to the top / right window closing icon alt + F4 (call setVisible (false) !?)

This is determined by the default close operation. You can set it using setDefaultCloseOperation . By default, HIDE_ON_CLOSE used, so you get a componentHidden call. If you set it to DISPOSE_ON_CLOSE , then you will get a windowClosed call. Setting the latter allows you to log only for these types of events.

Despite the fact that hiding and destruction do different things. Recycling frees resources used in hiding. In addition, hiding the last window will not exit the JVM while it is being removed.

As for the technical side of event scheduling, there are many subtleties. While removing a window invokes its hiding method, event scheduling is performed after operations are completed. This means that the EDT can send post-fact events. Since the window is closed, it does not send a hidden event.

+4
source

All Articles