Remove Top-Level Container on Runtime

后端 未结 5 1346
深忆病人
深忆病人 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条回答
  •  时光取名叫无心
    2020-11-21 14:27

    with the intent to blow away all doubts about EDT and confirm trashgod Updated suggestion, then output to the console is

    run:
    7163 KB used before GC
        Trying to Remove JDialog
        Remove Cycle Done :-)
    405 KB used after GC
        Checking if still exists any of TopLayoutContainers
    JFrame
    JDialog
        Will Try Remove Dialog again, CycleNo. 1
     -----------------------------------------------------------
    3274 KB used before GC
        Trying to Remove JDialog
        Remove Cycle Done :-)
    403 KB used after GC
        Checking if still exists any of TopLayoutContainers
    JFrame
    JDialog
        Will Try Remove Dialog again, CycleNo. 2
     -----------------------------------------------------------
    3271 KB used before GC
        Trying to Remove JDialog
        Remove Cycle Done :-)
    406 KB used after GC
        Checking if still exists any of TopLayoutContainers
    JFrame
    JDialog
        Will Try Remove Dialog again, CycleNo. 3
     -----------------------------------------------------------
    3275 KB used before GC
        Trying to Remove JDialog
        Remove Cycle Done :-)
    403 KB used after GC
        Checking if still exists any of TopLayoutContainers
    JFrame
    JDialog
     -----------------------------------------------------------
    *** End of Cycle Without Success, Exit App ***
    BUILD SUCCESSFUL (total time: 26 seconds) 
    

    from code

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowEvent;
    import javax.swing.*;
    
    public class RemoveDialogOnRuntime extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private int contID = 1;
        private boolean runProcess;
        private int top = 20;
        private int left = 20;
        private int maxLoop = 0;
        private javax.swing.Timer timer = null;
    
        public RemoveDialogOnRuntime() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(300, 300));
            setTitle("Remove Dialog On Runtime");
            setLocation(150, 150);
            pack();
            setVisible(true);
            Point loc = this.getLocation();
            top += loc.x;
            left += loc.y;
            AddNewDialog();
        }
    
        private void AddNewDialog() {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    DialogRemove firstDialog = new DialogRemove();
                    startAA();
                }
            });
        }
    
        private void startAA() {
            timer = new javax.swing.Timer(5000, updateAA());
            timer.setRepeats(false);
            timer.start();
        }
    
        public Action updateAA() {
            return new AbstractAction("text load action") {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.stop();
                    if (SwingUtilities.isEventDispatchThread()) {
                        Runnable doRun = new Runnable() {
    
                            @Override
                            public void run() {
                                remWins();
                            }
                        };
                        SwingUtilities.invokeLater(doRun);
                    } else {
                        Runnable doRun = new Runnable() {
    
                            @Override
                            public void run() {
                                remWins();
                            }
                        };
                        SwingUtilities.invokeLater(doRun);
                    }
                }
            };
        }
    
        private void remWins() {
            Runtime runtime = Runtime.getRuntime();
            long total = runtime.totalMemory();
            long free = runtime.freeMemory();
            long max = runtime.maxMemory();
            long used = total - free;
            System.out.println(Math.round(used / 1e3) + " KB used before GC");
            Window[] wins = Window.getWindows();
            for (int i = 0; i < wins.length; i++) {
                if (wins[i] instanceof JDialog) {
                    System.out.println("    Trying to Remove JDialog");
                    wins[i].setVisible(false);
                    wins[i].dispose();
                    WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING);
                    wins[i].dispatchEvent(windowClosing);
                    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
                    runtime = Runtime.getRuntime();
                    runtime.gc();
                    runtime.runFinalization();
                }
            }
            wins = null;
            System.out.println("    Remove Cycle Done :-)");
            runtime.runFinalization();
            runtime.gc();
            runtime = Runtime.getRuntime();
            total = runtime.totalMemory();
            free = runtime.freeMemory();
            max = runtime.maxMemory();
            used = total - free;
            System.out.println(Math.round(used / 1e3) + " KB used after GC");
            startOO();
        }
    
        private void startOO() {
            timer = new javax.swing.Timer(5000, updateOO());
            timer.setRepeats(false);
            timer.start();
        }
    
        public Action updateOO() {
            return new AbstractAction("text load action") {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.stop();
                    timer.stop();
                    if (SwingUtilities.isEventDispatchThread()) {
                        Runnable doRun = new Runnable() {//really contraproductive just dealayed
    
                            @Override
                            public void run() {
                                pastRemWins();
                            }
                        };
                        SwingUtilities.invokeLater(doRun);
                    } else {
                        Runnable doRun = new Runnable() {
    
                            @Override
                            public void run() {
                                pastRemWins();
                            }
                        };
                        SwingUtilities.invokeLater(doRun);
                    }
                }
            };
        }
    
        private void pastRemWins() {
            System.out.println("    Checking if still exists any of TopLayoutContainers");
            Window[] wins = Window.getWindows();
            for (int i = 0; i < wins.length; i++) {
                if (wins[i] instanceof JFrame) {
                    System.out.println("JFrame");
                    wins[i].setVisible(true);
                } else if (wins[i] instanceof JDialog) {
                    System.out.println("JDialog");
                    wins[i].setVisible(true);
                }
            }
            if (wins.length > 1) {
                wins = null;
                maxLoop++;
                if (maxLoop <= 3) {
                    System.out.println("    Will Try Remove Dialog again, CycleNo. " + maxLoop);
                    System.out.println(" -----------------------------------------------------------");
                    remWins();
                } else {
                    System.out.println(" -----------------------------------------------------------");
                    System.out.println("*** End of Cycle Without Success, Exit App ***");
                    closeMe();
                }
            }
            startAA();
        }
    
        private void closeMe() {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    System.exit(0);
                }
            });
        }
    
        private class DialogRemove extends JDialog {
    
            private static final long serialVersionUID = 1L;
    
            DialogRemove(final Frame parent) {
                super(parent, "SecondDialog " + (contID++));
                setLocation(top, left);
                top += 20;
                left += 20;
                setPreferredSize(new Dimension(200, 200));
                setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                setModalityType(Dialog.ModalityType.MODELESS);
                pack();
                setVisible(true);
            }
    
            private DialogRemove() {
                setTitle("SecondDialog " + (contID++));
                setLocation(top, left);
                top += 20;
                left += 20;
                setPreferredSize(new Dimension(200, 200));
                setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                setModalityType(Dialog.ModalityType.MODELESS);
                pack();
                setVisible(true);
            }
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    RemoveDialogOnRuntime superConstructor = new RemoveDialogOnRuntime();
                }
            });
        }
    }
    

提交回复
热议问题