How to destroy JDialog?

I am currently working on a core JFrame that always exists. I currently have a JDialog child that appears when a button is clicked. This frame has JMenu with an element to "exit the screen." I was instructed to ensure that this JDialog child JDialog when the exit button is pressed. When logging out, the main display is set invisible with:

 mainFrame.setVisible(false); 

The child JDialog has a default close operation:

 DISPONSE_ON_CLOSE 

When a user logs in, the first thing that was done:

 mainFrame.setVisible(true); 

When this happens, a dialog box appears for the child. Looking at the JDialog Javadoc, this seems to be the expected behavior. However, I did not find a way to break the parent / child release or completely destroy the JDialog child. It seems that JDialog will remain until there is a GC, which may not happen in a timely manner.

Here is an example program that models the behavior that I see:

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JToggleButton; import javax.swing.SwingUtilities; public class WindowTest { public static void createAndShowGUI() { JFrame aFrame = new JFrame("LAUNCHER"); final JFrame aParent = new JFrame("PARENT"); final JDialog aChild = new JDialog(aParent); aParent.setSize(200,200); final JToggleButton showParentButton = new JToggleButton("HIDE"); showParentButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showParentButton.setText(!showParentButton.isSelected() ? "SHOW": "HIDE"); aParent.setVisible(!showParentButton.isSelected()); } }); aChild.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); aChild.setSize(200,200); aParent.addComponentListener(new ComponentAdapter() { public void componentHidden(ComponentEvent e) { aChild.dispose(); aChild.setVisible(false); } }); aFrame.setContentPane(showParentButton); aFrame.pack(); aFrame.setVisible(true); aParent.setVisible(true); aChild.setVisible(true); } public static void main(String [] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { createAndShowGUI(); } }); } } 

When the parent is hidden, the child object is located. When the parent is displayed, the child shows the backup. Which is really strange when I click X on the child: when the parent is hidden and then shown again, the child does not display the backup.

The only difference I see is that pressing the X button also raises the WindowClosing event. I tried sending even, in the componentHidden method above:

 //Added into the constructor //add to the imports: import java.awt.event.WindowEvent; aParent.addComponentListener(new ComponentAdapter() { public void componentHidden(ComponentEvent e) { aChild.dispose(); aChild.setVisible(false); WindowEvent closingEvent = new WindowEvent(aChild, WindowEvent.WINDOW_CLOSING); aChild.dispatchEvent(closingEvent); } }); 

And that did not solve the problem.

Currently, it seems like my only option is to change the type of the child to a JFrame . I just wanted to know if there is a way to dispose of the JDialog child.

I am currently working with the Java version: 1.7.0_76 64 bit on Redhat Enterprise Linux Server 6.4.

+5
source share
1 answer

I did not know that naming conventions affected compilation.

This is not true. The conditions are met to ensure consistency, readability and maintainability. The person who writes the code is not always the person who maintains the code. Therefore, if you want other people to read your code, especially when asking for help, follow the standards.

You can start with the Java Style Guide

I manually copy the code from another screen in accordance with the standards of my companies.

This is a complete waste of time. There is nothing patented in your code. Again, when you ask a question, the code must be in SSCCE form so that it demonstrates the problem. This allows you to delete all unnecessary codes.

It would be correct to determine the import.

This is exactly how you should do it. You want us to help you, so why waste time on this? Make it as easy as people want to help you.

Adding imports did not help. The code you posted is not compiling yet, so I don’t know if it accurately reflects the problem you are trying to describe.

Again, the code sending point is that we can copy / paste / compile / test. Until you publish the correct SSCCE, I will not respond.

Edit:

From my testing, if the visibility of a child window changes with the visibility of the parent when the parent becomes invisible, then the visibility of the child also changes with the parent when it becomes visible. Thus, it looks like the parent keeps the state of the child windows when visibility changes.

Thus, the solution should make the child window invisible to the parent:

 showParentButton.setText(!showParentButton.isSelected() ? "SHOW": "HIDE"); aChild.setVisible(false); // add this aParent.setVisible(!showParentButton.isSelected()); 

If you don't have a link to the child window, I think you can use the Windows.getOwnedWindows() method to access all the child windows.

Other editing:

As a hack, I created my own dialog box, which cannot be displayed again after its placement:

  final JDialog aChild = new JDialog(aParent) { private boolean disposed = false; @Override public void dispose() { super.dispose(); disposed = true; } @Override public void show() { if (disposed) return; super.show(); } }; 
+4
source

All Articles