Remove Top-Level Container on Runtime

后端 未结 5 1311
深忆病人
深忆病人 2020-11-21 13:46

Unfortunately, it looks like this recently closed question was not well understood. Here is the typical output:

run:
    Trying to Remove JDialog
    Remove          


        
5条回答
  •  猫巷女王i
    2020-11-21 14:03

    I'm not sure if you question is about "garbage collection" or about how to identify dialogs that are visible.

    You can't control when garbage collection is done. Invoking the gc() method is only a suggestion.

    If you want to ignore "disposed" dialogs then you can use the isDisplayable() method to check its status.

    With the following program I got some interesting results. First change I made was to add some components to the dialog so that more resources would be used for each dialog which would increase the chance that the resources would be garbage collected.

    On my machine I found that if I

    a) create 5 dialogs
    b) close the dialogs
    c) create 5 dialogs

    Then the first 5 appear to be garbage collected.

    However if I create 5, then close then create 1, then close, it doesn't seem to work.

    Bottom line is you can't depend on when garbage collection will be done, so I suggest you use the isDisplayable() method to determine how to do your processing. The "Display Dialogs" button uses this method as part of the displayed output.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DialogSSCCE extends JPanel
    {
        public static int count;
    
        public DialogSSCCE()
        {
            JButton display = new JButton("Display Dialogs");
            display.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println();
                    System.out.println("Display Dialogs");
    
                    for (Window window: Window.getWindows())
                    {
                        if (window instanceof JDialog)
                        {
                            JDialog dialog = (JDialog)window;
                            System.out.println("\t" + dialog.getTitle() + " " + dialog.isDisplayable());
                        }
                    }
                }
            });
            add( display );
    
            JButton open = new JButton("Create Dialog");
            open.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println();
                    System.out.println("Create Dialog");
    
                    JDialog dialog = new JDialog();
                    dialog.getContentPane().setLayout(null);
    
                    for (int i = 0; i < 200; i++)
                    {
                        dialog.add( new JTextField("some text") );
                    }
    
                    dialog.setTitle("Dialog " + count++);
                    dialog.setLocation(count * 25, count * 25);
                    dialog.setVisible(true);
                    System.out.println("\tCreated " + dialog.getTitle());
                }
            });
            add( open );
    
            JButton close = new JButton("Close Dialogs");
            close.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println();
                    System.out.println("Close Dialogs");
    
                    for (Window window: Window.getWindows())
                    {
                        if (window instanceof JDialog)
                        {
                            JDialog dialog = (JDialog)window;
                            System.out.println("\tClosing " + dialog.getTitle());
                            dialog.dispose();
                        }
                    }
    
                    Runtime.getRuntime().gc();
                }
            });
            add( close );
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("DialogSSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new DialogSSCCE() );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

提交回复
热议问题