8234149: Several regression tests do not dispose Frame at end

Reviewed-by: serb
diff --git a/test/jdk/javax/swing/AbstractButton/6711682/bug6711682.java b/test/jdk/javax/swing/AbstractButton/6711682/bug6711682.java
index 72808d4..f509b55 100644
--- a/test/jdk/javax/swing/AbstractButton/6711682/bug6711682.java
+++ b/test/jdk/javax/swing/AbstractButton/6711682/bug6711682.java
@@ -43,53 +43,58 @@
     private static JCheckBox editorCb;
     private static JCheckBox rendererCb;
     private static JTable table;
+    private static JFrame f;
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+            robot.waitForIdle();
+            Point l = table.getLocationOnScreen();
+            int h = table.getRowHeight();
+            for (int i = 0; i < 3; i++) {
+                robot.mouseMove(l.x + 5, l.y + 5 + i * h);
+                robot.mousePress(InputEvent.BUTTON1_MASK);
+                robot.mouseRelease(InputEvent.BUTTON1_MASK);
             }
-        });
-        robot.waitForIdle();
-        Point l = table.getLocationOnScreen();
-        int h = table.getRowHeight();
-        for (int i = 0; i < 3; i++) {
-            robot.mouseMove(l.x + 5, l.y + 5 + i * h);
-            robot.mousePress(InputEvent.BUTTON1_MASK);
-            robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        }
-        // Without pressing F2 the last table's cell
-        // reported <code>false</code> value
-        // note that I can't press it inside the for loop
-        // because it doesn't reproduce the bug
-        robot.keyPress(KeyEvent.VK_F2);
-        robot.keyRelease(KeyEvent.VK_F2);
+            // Without pressing F2 the last table's cell
+            // reported <code>false</code> value
+            // note that I can't press it inside the for loop
+            // because it doesn't reproduce the bug
+            robot.keyPress(KeyEvent.VK_F2);
+            robot.keyRelease(KeyEvent.VK_F2);
 
-        for (int i = 0; i < 3; i++) {
-            if (!Boolean.TRUE.equals(table.getValueAt(i, 0))) {
-                throw new RuntimeException("Row #" + i + " checkbox is not selected");
+            for (int i = 0; i < 3; i++) {
+                if (!Boolean.TRUE.equals(table.getValueAt(i, 0))) {
+                    throw new RuntimeException("Row #" + i + " checkbox is not selected");
+                }
             }
-        }
-        for (int i = 2; i >= 0; i--) {
-            robot.mouseMove(l.x + 5, l.y + 5 + i * h);
-            robot.mousePress(InputEvent.BUTTON1_MASK);
-            robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        }
-        robot.keyPress(KeyEvent.VK_F2);
-        robot.keyRelease(KeyEvent.VK_F2);
-        for (int i = 0; i < 3; i++) {
-            if (Boolean.TRUE.equals(table.getValueAt(i, 0))) {
-                throw new RuntimeException("Row #" + i + " checkbox is selected");
+            for (int i = 2; i >= 0; i--) {
+                robot.mouseMove(l.x + 5, l.y + 5 + i * h);
+                robot.mousePress(InputEvent.BUTTON1_MASK);
+                robot.mouseRelease(InputEvent.BUTTON1_MASK);
             }
+            robot.keyPress(KeyEvent.VK_F2);
+            robot.keyRelease(KeyEvent.VK_F2);
+            for (int i = 0; i < 3; i++) {
+                if (Boolean.TRUE.equals(table.getValueAt(i, 0))) {
+                    throw new RuntimeException("Row #" + i + " checkbox is selected");
+                }
+            }
+        } finally {
+            if (f != null) SwingUtilities.invokeAndWait(() -> f.dispose());
         }
     }
 
     private static void createAndShowGUI() {
         editorCb = new JCheckBox();
         rendererCb = new JCheckBox();
-        JFrame f = new JFrame("Table with CheckBox");
+        f = new JFrame("Table with CheckBox");
         Container p = f.getContentPane();
         p.setLayout(new BorderLayout());
         table = new JTable(new Object[][]{{false}, {false}, {false}}, new Object[]{"CheckBox"});
diff --git a/test/jdk/javax/swing/Action/8133039/bug8133039.java b/test/jdk/javax/swing/Action/8133039/bug8133039.java
index cec80f2..766a712 100644
--- a/test/jdk/javax/swing/Action/8133039/bug8133039.java
+++ b/test/jdk/javax/swing/Action/8133039/bug8133039.java
@@ -45,10 +45,15 @@
 
     private static volatile int ACTION_PERFORMED_CALLS = 0;
     private static volatile int ACTION_ACCEPTED_CALLS = 0;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        testActionNotification();
-        testPopupAction();
+        try {
+            testActionNotification();
+            testPopupAction();
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     private static void testActionNotification() {
@@ -114,7 +119,7 @@
 
     private static void createAndShowGUI() {
 
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setSize(300, 300);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
diff --git a/test/jdk/javax/swing/JButton/4368790/bug4368790.java b/test/jdk/javax/swing/JButton/4368790/bug4368790.java
index a071fb5..8643274 100644
--- a/test/jdk/javax/swing/JButton/4368790/bug4368790.java
+++ b/test/jdk/javax/swing/JButton/4368790/bug4368790.java
@@ -36,9 +36,10 @@
 
 public class bug4368790 {
     private static JButton b1;
+    private static JFrame frame;
 
     private static void createGui() {
-        final JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setLayout(new FlowLayout());
 
@@ -53,21 +54,25 @@
     }
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                bug4368790.createGui();
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    bug4368790.createGui();
+                }
+            });
+            robot.waitForIdle();
+            robot.keyPress(KeyEvent.VK_SPACE);
+            robot.keyPress(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_SPACE);
+            robot.waitForIdle();
+            if (b1.getModel().isPressed()) {
+                throw new RuntimeException("The button is unexpectedly pressed");
             }
-        });
-        robot.waitForIdle();
-        robot.keyPress(KeyEvent.VK_SPACE);
-        robot.keyPress(KeyEvent.VK_TAB);
-        robot.keyRelease(KeyEvent.VK_TAB);
-        robot.keyRelease(KeyEvent.VK_SPACE);
-        robot.waitForIdle();
-        if (b1.getModel().isPressed()) {
-            throw new RuntimeException("The button is unexpectedly pressed");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JButton/4796987/bug4796987.java b/test/jdk/javax/swing/JButton/4796987/bug4796987.java
index 3280eff..62d11fc 100644
--- a/test/jdk/javax/swing/JButton/4796987/bug4796987.java
+++ b/test/jdk/javax/swing/JButton/4796987/bug4796987.java
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 4796987
+ * @key headful
  * @requires (os.family == "windows")
  * @summary XP Only: JButton.setBorderPainted() does not work with XP L&F
  * @author Alexander Scherbatiy
@@ -46,12 +47,17 @@
 
     private static JButton button1;
     private static JButton button2;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        if (Platform.isWindows()
+        try {
+            if (Platform.isWindows()
                 && OSVersion.current().equals(OSVersion.WINDOWS_XP)) {
-            UIManager.setLookAndFeel(new WindowsLookAndFeel());
-            testButtonBorder();
+                UIManager.setLookAndFeel(new WindowsLookAndFeel());
+                testButtonBorder();
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
@@ -89,7 +95,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame("Test");
+        frame = new JFrame("Test");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(200, 200);
 
diff --git a/test/jdk/javax/swing/JColorChooser/Test8051548.java b/test/jdk/javax/swing/JColorChooser/Test8051548.java
index 287601f..65e1776 100644
--- a/test/jdk/javax/swing/JColorChooser/Test8051548.java
+++ b/test/jdk/javax/swing/JColorChooser/Test8051548.java
@@ -56,11 +56,16 @@
     private static JColorChooser colorChooser;
     private static boolean propertyChangeListenerInvoked;
     private static volatile Color color;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        testColorPanels();
-        testShowDialog(true);
-        testShowDialog(false);
+        try {
+            testColorPanels();
+            testShowDialog(true);
+            testShowDialog(false);
+        } finally {
+            if(frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     private static void testColorPanels() throws Exception {
@@ -182,7 +187,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setSize(700, 500);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         colorChooser = new JColorChooser();
diff --git a/test/jdk/javax/swing/JComboBox/4523758/bug4523758.java b/test/jdk/javax/swing/JComboBox/4523758/bug4523758.java
index 52d1676..0c3fd5b 100644
--- a/test/jdk/javax/swing/JComboBox/4523758/bug4523758.java
+++ b/test/jdk/javax/swing/JComboBox/4523758/bug4523758.java
@@ -43,7 +43,7 @@
 
 public class bug4523758 {
 
-    private JFrame frame;
+    private static JFrame frame;
     private JToolBar tools;
     private JComboBox combo;
 
@@ -153,13 +153,15 @@
         }
     }
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         try {
             bug4523758 test = new bug4523758();
             test.doTest();
         } catch (Exception e) {
             e.printStackTrace();
             throw new RuntimeException("FAIL");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JComboBox/6236162/bug6236162.java b/test/jdk/javax/swing/JComboBox/6236162/bug6236162.java
index 56b85ff..31f145d 100644
--- a/test/jdk/javax/swing/JComboBox/6236162/bug6236162.java
+++ b/test/jdk/javax/swing/JComboBox/6236162/bug6236162.java
@@ -46,14 +46,18 @@
     private static MyComboUI comboUI;
 
     public static void main(String[] args) throws Exception {
-        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
-            }
-        });
-        test();
-        System.out.println("Test passed");
+        try {
+            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+            test();
+            System.out.println("Test passed");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     private static void createAndShowGUI() {
diff --git a/test/jdk/javax/swing/JComboBox/7195179/Test7195179.java b/test/jdk/javax/swing/JComboBox/7195179/Test7195179.java
index 7680bea..d5eb11c 100644
--- a/test/jdk/javax/swing/JComboBox/7195179/Test7195179.java
+++ b/test/jdk/javax/swing/JComboBox/7195179/Test7195179.java
@@ -42,43 +42,48 @@
  */
 
 public class Test7195179 {
+    private static JFrame frame;
     public static void main(String[] args) throws Exception {
         invokeAndWait(new Runnable() {
             @Override
             public void run() {
-                Integer[] items = {null, 1, 2, 3};
-                JComboBox<Integer> combo = new JComboBox<>(items);
-                JLabel label = new JLabel("choose:");
-                JPanel panel = new JPanel();
-                GroupLayout layout = new GroupLayout(panel);
-                panel.setLayout(layout);
-                label.setLabelFor(combo);
-                combo.setSelectedIndex(0);
-                combo.setRenderer(new ListCellRenderer<Integer>() {
-                    private final BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();
+                try {
+                    Integer[] items = {null, 1, 2, 3};
+                    JComboBox<Integer> combo = new JComboBox<>(items);
+                    JLabel label = new JLabel("choose:");
+                    JPanel panel = new JPanel();
+                    GroupLayout layout = new GroupLayout(panel);
+                    panel.setLayout(layout);
+                    label.setLabelFor(combo);
+                    combo.setSelectedIndex(0);
+                    combo.setRenderer(new ListCellRenderer<Integer>() {
+                        private final BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();
 
-                    @Override
-                    public Component getListCellRendererComponent(JList<? extends Integer> list, Integer value, int index, boolean isSelected, boolean cellHasFocus) {
-                        return this.renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
-                    }
-                });
-                layout.setAutoCreateContainerGaps(true);
-                layout.setAutoCreateGaps(true);
-                layout.setHorizontalGroup(layout.createSequentialGroup()
-                        .addGroup(layout.createParallelGroup().addComponent(label))
-                        .addGroup(layout.createParallelGroup().addComponent(combo)));
-                layout.setVerticalGroup(layout
-                        .createSequentialGroup()
-                        .addGroup(layout
+                        @Override
+                        public Component getListCellRendererComponent(JList<? extends Integer> list, Integer value, int index, boolean isSelected, boolean cellHasFocus) {
+                            return this.renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+                        }
+                    });
+                    layout.setAutoCreateContainerGaps(true);
+                    layout.setAutoCreateGaps(true);
+                    layout.setHorizontalGroup(layout.createSequentialGroup()
+                            .addGroup(layout.createParallelGroup().addComponent(label))
+                            .addGroup(layout.createParallelGroup().addComponent(combo)));
+                    layout.setVerticalGroup(layout
+                            .createSequentialGroup()
+                            .addGroup(layout
                                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
                                 .addComponent(label)
                                 .addComponent(combo)));
 
-                JFrame frame = new JFrame(getClass().getSimpleName());
-                frame.add(panel);
-                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-                frame.pack();
-                frame.setVisible(true);
+                    frame = new JFrame(getClass().getSimpleName());
+                    frame.add(panel);
+                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+                    frame.pack();
+                    frame.setVisible(true);
+                } finally {
+                    if (frame != null) frame.dispose();
+                }
             }
         });
     }
diff --git a/test/jdk/javax/swing/JComboBox/8057893/bug8057893.java b/test/jdk/javax/swing/JComboBox/8057893/bug8057893.java
index de8efce..5755d09 100644
--- a/test/jdk/javax/swing/JComboBox/8057893/bug8057893.java
+++ b/test/jdk/javax/swing/JComboBox/8057893/bug8057893.java
@@ -43,13 +43,14 @@
 public class bug8057893 {
 
     private static volatile boolean isComboBoxEdited = false;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
         Robot robot = new Robot();
         robot.setAutoDelay(50);
 
         EventQueue.invokeAndWait(() -> {
-            JFrame frame = new JFrame();
+            frame = new JFrame();
             frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
             JComboBox<String> comboBox = new JComboBox<>(new String[]{"one", "two"});
             comboBox.setEditable(true);
@@ -76,6 +77,7 @@
         robot.keyRelease(KeyEvent.VK_ENTER);
         robot.waitForIdle();
 
+        if (frame != null) EventQueue.invokeAndWait(() -> frame.dispose());
         if(!isComboBoxEdited){
             throw new RuntimeException("ComboBoxEdited event is not fired!");
         }
diff --git a/test/jdk/javax/swing/JComponent/4337267/bug4337267.java b/test/jdk/javax/swing/JComponent/4337267/bug4337267.java
index 83a08f8..b48c7bc 100644
--- a/test/jdk/javax/swing/JComponent/4337267/bug4337267.java
+++ b/test/jdk/javax/swing/JComponent/4337267/bug4337267.java
@@ -48,7 +48,7 @@
     TestJPanel p1, p2;
     TestBufferedImage i1, i2;
     JComponent[] printq;
-    JFrame window;
+    static JFrame window;
     static boolean testFailed = false;
     static boolean done = false;
 
@@ -246,33 +246,36 @@
             Graphics g0 = image.getGraphics();
             super.paint(g0);
             g.drawImage(image, 0, 0, this);
-        }
-    }
+        } }
 
 
 
     public static void main(String[] args) throws Throwable {
-        final bug4337267 test = new bug4337267();
-        SwingUtilities.invokeLater(new Runnable() {
-            public void run() {
-                test.run();
-            }
-        });
+        try {
+            final bug4337267 test = new bug4337267();
+            SwingUtilities.invokeLater(new Runnable() {
+                public void run() {
+                    test.run();
+                }
+            });
 
-         synchronized(test) {
-            while (!done) {
-                try {
-                    test.wait();
-                } catch (InterruptedException ex) {
-                    // do nothing
+             synchronized(test) {
+                while (!done) {
+                    try {
+                        test.wait();
+                    } catch (InterruptedException ex) {
+                        // do nothing
+                    }
                 }
             }
-        }
 
-        if (testFailed) {
-            throw new RuntimeException("FAIL");
-        }
+            if (testFailed) {
+                throw new RuntimeException("FAIL");
+            }
 
-        System.out.println("OK");
+            System.out.println("OK");
+        } finally {
+            if (window != null) SwingUtilities.invokeAndWait(() -> window.dispose());
+        }
     }
 }
diff --git a/test/jdk/javax/swing/JComponent/6989617/bug6989617.java b/test/jdk/javax/swing/JComponent/6989617/bug6989617.java
index ff6bd7f..4a50b96 100644
--- a/test/jdk/javax/swing/JComponent/6989617/bug6989617.java
+++ b/test/jdk/javax/swing/JComponent/6989617/bug6989617.java
@@ -36,68 +36,73 @@
 public class bug6989617 {
     private static MyPanel panel;
     private static JButton button;
+    private static JFrame frame;
 
     public static void main(String... args) throws Exception {
-        Robot robot = new Robot();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                JFrame frame = new JFrame();
-                frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                panel = new MyPanel();
+        try {
+            Robot robot = new Robot();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame();
+                    frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    panel = new MyPanel();
 
-                button = new JButton("Hello");
-                panel.add(button);
-                frame.add(panel);
+                    button = new JButton("Hello");
+                    panel.add(button);
+                    frame.add(panel);
 
-                frame.setSize(200, 300);
-                frame.setVisible(true);
-            }
-        });
-        // Testing the panel as a painting origin,
-        // the panel.paintImmediately() must be triggered
-        // when button.repaint() is called
-        robot.waitForIdle();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                panel.resetPaintRectangle();
-                button.repaint();
-            }
-        });
-        robot.waitForIdle();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                Rectangle pr = panel.getPaintRectangle();
-                if (!pr.getSize().equals(button.getSize())) {
-                    throw new RuntimeException("wrong size of the dirty area");
+                    frame.setSize(200, 300);
+                    frame.setVisible(true);
                 }
-                if (!pr.getLocation().equals(button.getLocation())) {
-                    throw new RuntimeException("wrong location of the dirty area");
+            });
+            // Testing the panel as a painting origin,
+            // the panel.paintImmediately() must be triggered
+            // when button.repaint() is called
+            robot.waitForIdle();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    panel.resetPaintRectangle();
+                    button.repaint();
                 }
-            }
-        });
-        // Testing the panel as NOT a painting origin
-        // the panel.paintImmediately() must NOT be triggered
-        // when button.repaint() is called
-        robot.waitForIdle();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                panel.resetPaintRectangle();
-                panel.setPaintingOrigin(false);
-                if (panel.getPaintRectangle() != null) {
-                    throw new RuntimeException("paint rectangle is not null");
+            });
+            robot.waitForIdle();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    Rectangle pr = panel.getPaintRectangle();
+                    if (!pr.getSize().equals(button.getSize())) {
+                        throw new RuntimeException("wrong size of the dirty area");
+                    }
+                    if (!pr.getLocation().equals(button.getLocation())) {
+                        throw new RuntimeException("wrong location of the dirty area");
+                    }
                 }
-                button.repaint();
-            }
-        });
-        robot.waitForIdle();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                if(panel.getPaintRectangle() != null) {
-                    throw new RuntimeException("paint rectangle is not null");
+            });
+            // Testing the panel as NOT a painting origin
+            // the panel.paintImmediately() must NOT be triggered
+            // when button.repaint() is called
+            robot.waitForIdle();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    panel.resetPaintRectangle();
+                    panel.setPaintingOrigin(false);
+                    if (panel.getPaintRectangle() != null) {
+                        throw new RuntimeException("paint rectangle is not null");
+                    }
+                    button.repaint();
                 }
-                System.out.println("Test passed...");
-            }
-        });
+            });
+            robot.waitForIdle();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    if(panel.getPaintRectangle() != null) {
+                        throw new RuntimeException("paint rectangle is not null");
+                    }
+                    System.out.println("Test passed...");
+                }
+            });
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     static class MyPanel extends JPanel {
diff --git a/test/jdk/javax/swing/JComponent/7154030/bug7154030.java b/test/jdk/javax/swing/JComponent/7154030/bug7154030.java
index 3cb587e..81a3548 100644
--- a/test/jdk/javax/swing/JComponent/7154030/bug7154030.java
+++ b/test/jdk/javax/swing/JComponent/7154030/bug7154030.java
@@ -56,115 +56,119 @@
 public class bug7154030 {
 
     private static JButton button = null;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        BufferedImage imageInit = null;
+        try {
+            BufferedImage imageInit = null;
 
-        BufferedImage imageShow = null;
+            BufferedImage imageShow = null;
 
-        BufferedImage imageHide = null;
+            BufferedImage imageHide = null;
 
-        ExtendedRobot robot = new ExtendedRobot();
+            ExtendedRobot robot = new ExtendedRobot();
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                JDesktopPane desktop = new JDesktopPane();
-                button = new JButton("button");
-                JFrame frame = new JFrame();
+                @Override
+                public void run() {
+                    JDesktopPane desktop = new JDesktopPane();
+                    button = new JButton("button");
+                    frame = new JFrame();
 
-                button.setSize(200, 200);
-                button.setLocation(100, 100);
-                button.setForeground(Color.RED);
-                button.setBackground(Color.RED);
-                button.setOpaque(true);
-                button.setVisible(false);
-                desktop.add(button);
+                    button.setSize(200, 200);
+                    button.setLocation(100, 100);
+                    button.setForeground(Color.RED);
+                    button.setBackground(Color.RED);
+                    button.setOpaque(true);
+                    button.setVisible(false);
+                    desktop.add(button);
 
-                frame.setContentPane(desktop);
-                frame.setSize(300, 300);
-                frame.setLocation(0, 0);
-                frame.setVisible(true);
-                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+                    frame.setContentPane(desktop);
+                    frame.setSize(300, 300);
+                    frame.setLocation(0, 0);
+                    frame.setVisible(true);
+                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+                }
+            });
+
+            robot.waitForIdle(500);
+            imageInit = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    button.show();
+                }
+            });
+
+            robot.waitForIdle(500);
+            imageShow = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
+            if (Util.compareBufferedImages(imageInit, imageShow)) {
+                throw new Exception("Failed to show opaque button");
             }
-        });
 
-        robot.waitForIdle(500);
-        imageInit = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
+            robot.waitForIdle();
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    button.hide();
+                }
+            });
 
-            @Override
-            public void run() {
-                button.show();
+            robot.waitForIdle(500);
+            imageHide = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
+
+            if (!Util.compareBufferedImages(imageInit, imageHide)) {
+                throw new Exception("Failed to hide opaque button");
             }
-        });
 
-        robot.waitForIdle(500);
-        imageShow = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
-        if (Util.compareBufferedImages(imageInit, imageShow)) {
-            throw new Exception("Failed to show opaque button");
-        }
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-        robot.waitForIdle();
+                @Override
+                public void run() {
+                    button.setOpaque(false);
+                    button.setBackground(new Color(128, 128, 0));
+                    button.setVisible(false);
+                }
+            });
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            robot.waitForIdle(500);
+            imageInit = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 
-            @Override
-            public void run() {
-                button.hide();
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    button.show();
+                }
+            });
+
+            robot.waitForIdle(500);
+            imageShow = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    button.hide();
+                }
+            });
+
+            if (Util.compareBufferedImages(imageInit, imageShow)) {
+                throw new Exception("Failed to show non-opaque button");
             }
-        });
 
-        robot.waitForIdle(500);
-        imageHide = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
+            robot.waitForIdle(500);
+            imageHide = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 
-        if (!Util.compareBufferedImages(imageInit, imageHide)) {
-            throw new Exception("Failed to hide opaque button");
-        }
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                button.setOpaque(false);
-                button.setBackground(new Color(128, 128, 0));
-                button.setVisible(false);
+            if (!Util.compareBufferedImages(imageInit, imageHide)) {
+                throw new Exception("Failed to hide non-opaque button");
             }
-        });
-
-        robot.waitForIdle(500);
-        imageInit = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                button.show();
-            }
-        });
-
-        robot.waitForIdle(500);
-        imageShow = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                button.hide();
-            }
-        });
-
-        if (Util.compareBufferedImages(imageInit, imageShow)) {
-            throw new Exception("Failed to show non-opaque button");
-        }
-
-        robot.waitForIdle(500);
-        imageHide = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
-
-        if (!Util.compareBufferedImages(imageInit, imageHide)) {
-            throw new Exception("Failed to hide non-opaque button");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JEditorPane/4492274/bug4492274.java b/test/jdk/javax/swing/JEditorPane/4492274/bug4492274.java
index 179450d..47b5bac 100644
--- a/test/jdk/javax/swing/JEditorPane/4492274/bug4492274.java
+++ b/test/jdk/javax/swing/JEditorPane/4492274/bug4492274.java
@@ -42,36 +42,40 @@
 
     private static JEditorPane jep;
 
+    private static JFrame f;
+
     public static void main(String args[]) throws Exception {
-
-        Robot robot = new Robot();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            @Override
-            public void run() {
-                createAndShowGUI();
-            }
-        });
-
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    page = new URL(page, "#linkname");
-                    jep.setPage(page);
-                } catch (Exception e) {
-                    throw new RuntimeException(e);
+        try {
+            Robot robot = new Robot();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    createAndShowGUI();
                 }
+            });
+
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        page = new URL(page, "#linkname");
+                        jep.setPage(page);
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+            });
+
+            robot.waitForIdle();
+
+            if (getPageAnchor() == null) {
+                throw new RuntimeException("JEditorPane.getPage() returns null anchor reference");
             }
-        });
-
-        robot.waitForIdle();
-
-        if (getPageAnchor() == null) {
-            throw new RuntimeException("JEditorPane.getPage() returns null anchor reference");
+        } finally {
+            if (f != null) SwingUtilities.invokeAndWait(() -> f.dispose());
         }
-
     }
 
     private static String getPageAnchor() throws Exception {
@@ -92,7 +96,7 @@
             File file = new File(System.getProperty("test.src", "."), "test.html");
             page = file.toURI().toURL();
 
-            JFrame f = new JFrame();
+            f = new JFrame();
 
             jep = new JEditorPane();
             jep.setEditorKit(new HTMLEditorKit());
diff --git a/test/jdk/javax/swing/JFrame/4962534/bug4962534.java b/test/jdk/javax/swing/JFrame/4962534/bug4962534.java
index aa8b4c6..789d780 100644
--- a/test/jdk/javax/swing/JFrame/4962534/bug4962534.java
+++ b/test/jdk/javax/swing/JFrame/4962534/bug4962534.java
@@ -39,17 +39,21 @@
     Robot robot;
     volatile Point framePosition;
     volatile Point newFrameLocation;
-    JFrame frame;
+    static JFrame frame;
     Rectangle gcBounds;
     Component titleComponent;
     JLayeredPane lPane;
     volatile boolean titleFound = false;
     public static Object LOCK = new Object();
 
-    public static void main(final String[] args) {
-        bug4962534 app = new bug4962534();
-        app.init();
-        app.start();
+    public static void main(final String[] args) throws Exception {
+        try {
+            bug4962534 app = new bug4962534();
+            app.init();
+            app.start();
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     public void init() {
diff --git a/test/jdk/javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java b/test/jdk/javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java
index 71aa923..1eb6641 100644
--- a/test/jdk/javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java
+++ b/test/jdk/javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java
@@ -42,46 +42,49 @@
     private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
+            SwingUtilities.invokeAndWait(() -> {
+                frame = new JFrame();
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                frame.setSize(400, 300);
+                JPanel panel = new JPanel();
+                panel.setBackground(BACKGROUND);
+                frame.getContentPane().add(panel);
+                frame.setVisible(true);
+            });
 
-        SwingUtilities.invokeAndWait(() -> {
-            frame = new JFrame();
-            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-            frame.setSize(400, 300);
-            JPanel panel = new JPanel();
-            panel.setBackground(BACKGROUND);
-            frame.getContentPane().add(panel);
-            frame.setVisible(true);
-        });
+            robot.waitForIdle();
+            Thread.sleep(200);
 
-        robot.waitForIdle();
-        Thread.sleep(200);
+            Rectangle[] rects = new Rectangle[1];
+            SwingUtilities.invokeAndWait(() -> {
+                rects[0] = frame.getBounds();
+            });
 
-        Rectangle[] rects = new Rectangle[1];
-        SwingUtilities.invokeAndWait(() -> {
-            rects[0] = frame.getBounds();
-        });
+            Rectangle bounds = rects[0];
 
-        Rectangle bounds = rects[0];
+            int x = bounds.x + bounds.width / 4;
+            int y = bounds.y + bounds.height / 4;
 
-        int x = bounds.x + bounds.width / 4;
-        int y = bounds.y + bounds.height / 4;
+            Color color = robot.getPixelColor(x, y);
 
-        Color color = robot.getPixelColor(x, y);
+            if (!BACKGROUND.equals(color)) {
+                throw new RuntimeException("Wrong backgound color!");
+            }
 
-        if (!BACKGROUND.equals(color)) {
-            throw new RuntimeException("Wrong backgound color!");
-        }
+            x = bounds.x + 3 * bounds.width / 4;
+            y = bounds.y + 3 * bounds.height / 4;
 
-        x = bounds.x + 3 * bounds.width / 4;
-        y = bounds.y + 3 * bounds.height / 4;
+            color = robot.getPixelColor(x, y);
 
-        color = robot.getPixelColor(x, y);
-
-        if (!BACKGROUND.equals(color)) {
-            throw new RuntimeException("Wrong backgound color!");
+            if (!BACKGROUND.equals(color)) {
+                throw new RuntimeException("Wrong backgound color!");
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JInternalFrame/5066752/bug5066752.java b/test/jdk/javax/swing/JInternalFrame/5066752/bug5066752.java
index 4182cca..c813a14 100644
--- a/test/jdk/javax/swing/JInternalFrame/5066752/bug5066752.java
+++ b/test/jdk/javax/swing/JInternalFrame/5066752/bug5066752.java
@@ -39,20 +39,24 @@
     private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        Robot r = new Robot();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
+        try {
+            Robot r = new Robot();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+            r.waitForIdle();
+
+            r.delay(600);
+
+            Point p = Util.getCenterPoint(frame);
+            Color color =  r.getPixelColor((int) p.getX(), (int) p.getY());
+            if (!color.equals(Color.RED)) {
+                throw new Exception("Test failed: JDesktopPane isn't transparent. Expected color is (red color): " + Color.RED + ", actual color is: " + color);
             }
-        });
-        r.waitForIdle();
-
-        r.delay(600);
-
-        Point p = Util.getCenterPoint(frame);
-        Color color =  r.getPixelColor((int) p.getX(), (int) p.getY());
-        if (!color.equals(Color.RED)) {
-            throw new Exception("Test failed: JDesktopPane isn't transparent. Expected color is (red color): " + Color.RED + ", actual color is: " + color);
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JInternalFrame/8160248/JInternalFrameDraggingTest.java b/test/jdk/javax/swing/JInternalFrame/8160248/JInternalFrameDraggingTest.java
index 7b245dd..81f5e0d 100644
--- a/test/jdk/javax/swing/JInternalFrame/8160248/JInternalFrameDraggingTest.java
+++ b/test/jdk/javax/swing/JInternalFrame/8160248/JInternalFrameDraggingTest.java
@@ -53,32 +53,35 @@
     private static Color BACKGROUND_COLOR = Color.ORANGE;
 
     public static void main(String[] args) throws Exception {
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(20);
+            SwingUtilities.invokeAndWait(JInternalFrameDraggingTest::createAndShowGUI);
+            robot.waitForIdle();
 
-        Robot robot = new Robot();
-        robot.setAutoDelay(20);
-        SwingUtilities.invokeAndWait(JInternalFrameDraggingTest::createAndShowGUI);
-        robot.waitForIdle();
+            final int translate = FRAME_SIZE / 4;
+            moveFrame(robot, translate, translate / 2, translate / 2);
+            robot.waitForIdle();
 
-        final int translate = FRAME_SIZE / 4;
-        moveFrame(robot, translate, translate / 2, translate / 2);
-        robot.waitForIdle();
+            Point p = getDesktopPaneLocation();
+            int size = translate / 2;
+            Rectangle rect = new Rectangle(p.x, p.y, size, size);
+            BufferedImage img = robot.createScreenCapture(rect);
 
-        Point p = getDesktopPaneLocation();
-        int size = translate / 2;
-        Rectangle rect = new Rectangle(p.x, p.y, size, size);
-        BufferedImage img = robot.createScreenCapture(rect);
-
-        int testRGB = BACKGROUND_COLOR.getRGB();
-        for (int i = 1; i < size; i++) {
-            int rgbCW = img.getRGB(i, size / 2);
-            int rgbCH = img.getRGB(size / 2, i);
-            if (rgbCW != testRGB || rgbCH != testRGB) {
-                System.out.println("i " + i + " rgbCW " +
-                                   Integer.toHexString(rgbCW) +
-                                   " testRGB " + Integer.toHexString(testRGB) +
-                                   " rgbCH " + Integer.toHexString(rgbCH));
-                throw new RuntimeException("Background color is wrong!");
+            int testRGB = BACKGROUND_COLOR.getRGB();
+            for (int i = 1; i < size; i++) {
+                int rgbCW = img.getRGB(i, size / 2);
+                int rgbCH = img.getRGB(size / 2, i);
+                if (rgbCW != testRGB || rgbCH != testRGB) {
+                    System.out.println("i " + i + " rgbCW " +
+                                       Integer.toHexString(rgbCW) +
+                                       " testRGB " + Integer.toHexString(testRGB) +
+                                       " rgbCH " + Integer.toHexString(rgbCH));
+                    throw new RuntimeException("Background color is wrong!");
+                }
             }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java b/test/jdk/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java
index df332e2..8e38c72 100644
--- a/test/jdk/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java
+++ b/test/jdk/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java
@@ -40,35 +40,40 @@
     public static final int waitTime = 5000;
     private static Robot robot;
     private static CustomInternalFrame iFrame;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        initRobot();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            @Override
-            public void run() {
-                initUI();
-                try {
-                    closeInternalFrame();
-                } catch (PropertyVetoException e) {
-                    throw new RuntimeException(e);
+        try {
+            initRobot();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    initUI();
+                    try {
+                        closeInternalFrame();
+                    } catch (PropertyVetoException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+            });
+            robot.waitForIdle();
+            invokeGC();
+            System.runFinalization();
+            Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later
+            Date startWaiting = new Date();
+            synchronized (CustomInternalFrame.waiter) {
+                // Sync with finalization thread.
+                Date now = new Date();
+                while (now.getTime() - startWaiting.getTime() < maxWaitTime && !CustomInternalFrame.finalized) {
+                    CustomInternalFrame.waiter.wait(waitTime);
+                    now = new Date();
                 }
             }
-        });
-        robot.waitForIdle();
-        invokeGC();
-        System.runFinalization();
-        Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later
-        Date startWaiting = new Date();
-        synchronized (CustomInternalFrame.waiter) {
-            // Sync with finalization thread.
-            Date now = new Date();
-            while (now.getTime() - startWaiting.getTime() < maxWaitTime && !CustomInternalFrame.finalized) {
-                CustomInternalFrame.waiter.wait(waitTime);
-                now = new Date();
+            if (!CustomInternalFrame.finalized) {
+                throw new RuntimeException("Closed internal frame wasn't collected");
             }
-        }
-        if (!CustomInternalFrame.finalized) {
-            throw new RuntimeException("Closed internal frame wasn't collected");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
@@ -83,7 +88,7 @@
     }
 
     private static void initUI() {
-        JFrame frame = new JFrame("Internal Frame Test");
+        frame = new JFrame("Internal Frame Test");
         frame.getContentPane().setLayout(new BorderLayout());
         JDesktopPane desktopPane = new JDesktopPane();
         desktopPane.setDesktopManager(new DefaultDesktopManager());
diff --git a/test/jdk/javax/swing/JLabel/6596966/bug6596966.java b/test/jdk/javax/swing/JLabel/6596966/bug6596966.java
index 6a85f63..2d31759 100644
--- a/test/jdk/javax/swing/JLabel/6596966/bug6596966.java
+++ b/test/jdk/javax/swing/JLabel/6596966/bug6596966.java
@@ -49,65 +49,69 @@
     private static JComboBox comboBox;
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                button = new JButton("Button");
-                comboBox = new JComboBox();
-
-                label = new JLabel("Label");
-                label.setDisplayedMnemonic('L');
-                label.setLabelFor(comboBox);
-
-                JPanel pnContent = new JPanel();
-
-                pnContent.add(button);
-                pnContent.add(label);
-                pnContent.add(comboBox);
-
-                frame = new JFrame();
-
-                frame.add(pnContent);
-                frame.pack();
-                frame.setVisible(true);
-            }
-        });
-
-        robot.waitForIdle();
-
-
-        int keyMask = InputEvent.ALT_MASK;
-        if (Platform.isOSX()) {
-            keyMask = InputEvent.CTRL_MASK | InputEvent.ALT_MASK;
-        }
-        ArrayList<Integer> keys = Util.getKeyCodesFromKeyMask(keyMask);
-        for (int i = 0; i < keys.size(); ++i) {
-            robot.keyPress(keys.get(i));
-        }
-
-        robot.keyPress(KeyEvent.VK_L);
-
-        robot.waitForIdle();
-        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new KeyEvent(label, KeyEvent.KEY_RELEASED,
-                EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_L, 'L'));
-
-        robot.waitForIdle();
-
         try {
+            Robot robot = new Robot();
+
             SwingUtilities.invokeAndWait(new Runnable() {
                 public void run() {
-                    if (!comboBox.isFocusOwner()) {
-                        throw new RuntimeException("comboBox isn't focus owner");
-                    }
+                    button = new JButton("Button");
+                    comboBox = new JComboBox();
+
+                    label = new JLabel("Label");
+                    label.setDisplayedMnemonic('L');
+                    label.setLabelFor(comboBox);
+
+                    JPanel pnContent = new JPanel();
+
+                    pnContent.add(button);
+                    pnContent.add(label);
+                    pnContent.add(comboBox);
+
+                    frame = new JFrame();
+
+                    frame.add(pnContent);
+                    frame.pack();
+                    frame.setVisible(true);
                 }
             });
-        } finally {
-            robot.keyRelease(KeyEvent.VK_L);
-            for (int i = 0; i < keys.size(); ++i) {
-                robot.keyRelease(keys.get(i));
-            }
+
             robot.waitForIdle();
+
+
+            int keyMask = InputEvent.ALT_MASK;
+            if (Platform.isOSX()) {
+                keyMask = InputEvent.CTRL_MASK | InputEvent.ALT_MASK;
+            }
+            ArrayList<Integer> keys = Util.getKeyCodesFromKeyMask(keyMask);
+            for (int i = 0; i < keys.size(); ++i) {
+                robot.keyPress(keys.get(i));
+            }
+
+            robot.keyPress(KeyEvent.VK_L);
+
+            robot.waitForIdle();
+            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new KeyEvent(label, KeyEvent.KEY_RELEASED,
+                    EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_L, 'L'));
+
+            robot.waitForIdle();
+
+            try {
+                SwingUtilities.invokeAndWait(new Runnable() {
+                    public void run() {
+                        if (!comboBox.isFocusOwner()) {
+                            throw new RuntimeException("comboBox isn't focus owner");
+                        }
+                    }
+                });
+            } finally {
+                robot.keyRelease(KeyEvent.VK_L);
+                for (int i = 0; i < keys.size(); ++i) {
+                    robot.keyRelease(keys.get(i));
+                }
+                robot.waitForIdle();
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JLayer/6824395/bug6824395.java b/test/jdk/javax/swing/JLayer/6824395/bug6824395.java
index ab46d73..899e862 100644
--- a/test/jdk/javax/swing/JLayer/6824395/bug6824395.java
+++ b/test/jdk/javax/swing/JLayer/6824395/bug6824395.java
@@ -40,48 +40,53 @@
 public class bug6824395 {
 
     static JScrollPane scrollPane;
+    static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                JFrame frame = new JFrame("testing");
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-                JEditorPane editorPane = new JEditorPane();
-                String str = "hello\n";
-                for(int i = 0; i<5; i++) {
-                    str += str;
-                }
-
-                editorPane.setText(str);
-
-                JLayer<JEditorPane> editorPaneLayer = new JLayer<JEditorPane>(editorPane);
-                LayerUI<JComponent> layerUI = new LayerUI<JComponent>();
-                editorPaneLayer.setUI(layerUI);
-
-                scrollPane = new JScrollPane(editorPaneLayer);
-                scrollPane.setViewportBorder(null);
-                scrollPane.setPreferredSize(new Dimension(200, 250));
-                frame.add(scrollPane);
-
-                frame.setSize(200, 200);
-                frame.pack();
-                frame.setVisible(true);
-            }
-        });
         try {
-             ExtendedRobot robot = new ExtendedRobot();
-             robot.waitForIdle(300);
-         }catch(Exception ex) {
-             ex.printStackTrace();
-             throw new Error("Unexpected Failure");
-         }
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                if (scrollPane.getViewportBorderBounds().width != scrollPane.getViewport().getView().getWidth()) {
-                    throw new RuntimeException("Wrong component's width!");
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame("testing");
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+                    JEditorPane editorPane = new JEditorPane();
+                    String str = "hello\n";
+                    for(int i = 0; i<5; i++) {
+                        str += str;
+                    }
+
+                    editorPane.setText(str);
+
+                    JLayer<JEditorPane> editorPaneLayer = new JLayer<JEditorPane>(editorPane);
+                    LayerUI<JComponent> layerUI = new LayerUI<JComponent>();
+                    editorPaneLayer.setUI(layerUI);
+
+                    scrollPane = new JScrollPane(editorPaneLayer);
+                    scrollPane.setViewportBorder(null);
+                    scrollPane.setPreferredSize(new Dimension(200, 250));
+                    frame.add(scrollPane);
+
+                    frame.setSize(200, 200);
+                    frame.pack();
+                    frame.setVisible(true);
                 }
-            }
-        });
+            });
+            try {
+                 ExtendedRobot robot = new ExtendedRobot();
+                 robot.waitForIdle(300);
+             }catch(Exception ex) {
+                 ex.printStackTrace();
+                 throw new Error("Unexpected Failure");
+             }
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    if (scrollPane.getViewportBorderBounds().width != scrollPane.getViewport().getView().getWidth()) {
+                        throw new RuntimeException("Wrong component's width!");
+                    }
+                }
+            });
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 }
diff --git a/test/jdk/javax/swing/JList/6462008/bug6462008.java b/test/jdk/javax/swing/JList/6462008/bug6462008.java
index 77febbc..d3058c8 100644
--- a/test/jdk/javax/swing/JList/6462008/bug6462008.java
+++ b/test/jdk/javax/swing/JList/6462008/bug6462008.java
@@ -42,33 +42,38 @@
     private static int controlKey;
     private static JList list;
     private static Robot robot;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        robot = new Robot();
-        robot.setAutoDelay(100);
+        try {
+            robot = new Robot();
+            robot.setAutoDelay(100);
 
-        isAquaLAF = "Aqua".equals(UIManager.getLookAndFeel().getID());
-        controlKey = isAquaLAF ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
+            isAquaLAF = "Aqua".equals(UIManager.getLookAndFeel().getID());
+            controlKey = isAquaLAF ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                createAndShowGUI();
-            }
-        });
+                @Override
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        setAnchorLead(-1);
-        robot.waitForIdle();
+            setAnchorLead(-1);
+            robot.waitForIdle();
 
-        testListSelection();
+            testListSelection();
 
-        setAnchorLead(100);
-        robot.waitForIdle();
+            setAnchorLead(100);
+            robot.waitForIdle();
 
-        testListSelection();
+            testListSelection();
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     public static void testListSelection() throws Exception {
@@ -351,7 +356,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame("bug6462008");
+        frame = new JFrame("bug6462008");
         frame.setSize(200, 500);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
diff --git a/test/jdk/javax/swing/JList/6510999/bug6510999.java b/test/jdk/javax/swing/JList/6510999/bug6510999.java
index 1269de2..af76321 100644
--- a/test/jdk/javax/swing/JList/6510999/bug6510999.java
+++ b/test/jdk/javax/swing/JList/6510999/bug6510999.java
@@ -35,9 +35,10 @@
 
 public class bug6510999 {
     private static JScrollPane s;
+    static JFrame frame;
 
     private static void createGui() {
-        final JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         DefaultListModel dlm = new DefaultListModel();
@@ -57,20 +58,24 @@
     }
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(10);
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                bug6510999.createGui();
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(10);
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    bug6510999.createGui();
+                }
+            });
+            robot.waitForIdle();
+            Point viewPosition = s.getViewport().getViewPosition();
+            robot.keyPress(KeyEvent.VK_DOWN);
+            robot.keyRelease(KeyEvent.VK_DOWN);
+            robot.waitForIdle();
+            if (!s.getViewport().getViewPosition().equals(viewPosition)) {
+                throw new RuntimeException("JScrollPane was unexpectedly scrolled");
             }
-        });
-        robot.waitForIdle();
-        Point viewPosition = s.getViewport().getViewPosition();
-        robot.keyPress(KeyEvent.VK_DOWN);
-        robot.keyRelease(KeyEvent.VK_DOWN);
-        robot.waitForIdle();
-        if (!s.getViewport().getViewPosition().equals(viewPosition)) {
-            throw new RuntimeException("JScrollPane was unexpectedly scrolled");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JMenu/4417601/bug4417601.java b/test/jdk/javax/swing/JMenu/4417601/bug4417601.java
index ce1c8ea..cd8b7ec 100644
--- a/test/jdk/javax/swing/JMenu/4417601/bug4417601.java
+++ b/test/jdk/javax/swing/JMenu/4417601/bug4417601.java
@@ -39,6 +39,7 @@
 public class bug4417601 {
     static JMenu menu;
     static volatile boolean flag;
+    static JFrame frame;
 
     public static void main(String[] args) throws Exception {
 
@@ -47,7 +48,7 @@
 
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
-                JFrame frame = new JFrame();
+                frame = new JFrame();
                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 menu = new JMenu("Menu");
                 JMenuBar bar = new JMenuBar();
@@ -75,6 +76,7 @@
         robot.mousePress(InputEvent.BUTTON1_MASK);
         robot.mouseRelease(InputEvent.BUTTON1_MASK);
         robot.waitForIdle();
+        if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         if (flag) {
             throw new RuntimeException("Empty popup was shown");
         }
diff --git a/test/jdk/javax/swing/JMenu/4515762/bug4515762.java b/test/jdk/javax/swing/JMenu/4515762/bug4515762.java
index 67387c8..74bd0d9 100644
--- a/test/jdk/javax/swing/JMenu/4515762/bug4515762.java
+++ b/test/jdk/javax/swing/JMenu/4515762/bug4515762.java
@@ -39,6 +39,7 @@
 
     private static volatile boolean actionExpected = false;
     private static volatile boolean actionRecieved = false;
+    private static JFrame frame;
 
     /**
      * @param str name of Menu
@@ -107,65 +108,69 @@
     }
 
     public static void main(String[] args) throws Throwable {
-        Robot robot = new Robot();
-        robot.setAutoDelay(250);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(250);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                JFrame frame = new JFrame("Test");
-                frame.setJMenuBar(createMenuBar());
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                frame.pack();
-                frame.setVisible(true);
+                @Override
+                public void run() {
+                    frame = new JFrame("Test");
+                    frame.setJMenuBar(createMenuBar());
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    frame.pack();
+                    frame.setVisible(true);
+                }
+            });
+
+            robot.waitForIdle();
+
+            Util.hitMnemonics(robot, KeyEvent.VK_D);
+            robot.waitForIdle();
+
+            // Press the S key many times (should not cause an action peformed)
+            int TIMES = 5;
+            for (int i = 0; i < TIMES; i++) {
+                Util.hitKeys(robot, KeyEvent.VK_S);
             }
-        });
+            robot.waitForIdle();
 
-        robot.waitForIdle();
+            // Unique menu items.
+            actionExpected = true;
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
 
-        Util.hitMnemonics(robot, KeyEvent.VK_D);
-        robot.waitForIdle();
+            robot.keyPress(KeyEvent.VK_S);
+            robot.keyRelease(KeyEvent.VK_S);
+            robot.waitForIdle();
 
-        // Press the S key many times (should not cause an action peformed)
-        int TIMES = 5;
-        for (int i = 0; i < TIMES; i++) {
-            Util.hitKeys(robot, KeyEvent.VK_S);
+            checkAction();
+
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
+            robot.keyPress(KeyEvent.VK_M);
+            robot.keyRelease(KeyEvent.VK_M);
+            robot.waitForIdle();
+
+            checkAction();
+
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
+            Util.hitKeys(robot, KeyEvent.VK_T);
+            robot.waitForIdle();
+
+            checkAction();
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
+            Util.hitKeys(robot, KeyEvent.VK_W);
+            robot.waitForIdle();
+
+            checkAction();
+
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
+            Util.hitKeys(robot, KeyEvent.VK_U);
+            robot.waitForIdle();
+
+            checkAction();
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
-        robot.waitForIdle();
-
-        // Unique menu items.
-        actionExpected = true;
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-
-        robot.keyPress(KeyEvent.VK_S);
-        robot.keyRelease(KeyEvent.VK_S);
-        robot.waitForIdle();
-
-        checkAction();
-
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-        robot.keyPress(KeyEvent.VK_M);
-        robot.keyRelease(KeyEvent.VK_M);
-        robot.waitForIdle();
-
-        checkAction();
-
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-        Util.hitKeys(robot, KeyEvent.VK_T);
-        robot.waitForIdle();
-
-        checkAction();
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-        Util.hitKeys(robot, KeyEvent.VK_W);
-        robot.waitForIdle();
-
-        checkAction();
-
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-        Util.hitKeys(robot, KeyEvent.VK_U);
-        robot.waitForIdle();
-
-        checkAction();
     }
 }
diff --git a/test/jdk/javax/swing/JMenu/4692443/bug4692443.java b/test/jdk/javax/swing/JMenu/4692443/bug4692443.java
index 9d52215..d61da32 100644
--- a/test/jdk/javax/swing/JMenu/4692443/bug4692443.java
+++ b/test/jdk/javax/swing/JMenu/4692443/bug4692443.java
@@ -42,42 +42,45 @@
     public static PassedListener pass;
     public static FailedListener fail;
     public static volatile Boolean passed;
+    public static JFrame mainFrame;
 
     public static void main(String args[]) throws Throwable {
+        try {
+            fail = new FailedListener();
+            pass = new PassedListener();
+            passed = false;
+            Robot robo = new Robot();
 
-        fail = new FailedListener();
-        pass = new PassedListener();
-        passed = false;
-        Robot robo = new Robot();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
+            robo.waitForIdle();
+
+            int altKey = java.awt.event.KeyEvent.VK_ALT;
+            robo.setAutoDelay(100);
+            Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu
+            robo.keyPress(KeyEvent.VK_S);  // Enter submenu
+            robo.keyRelease(KeyEvent.VK_S);
+            robo.keyPress(KeyEvent.VK_O); // Launch "One" action
+            robo.keyRelease(KeyEvent.VK_O);
+            robo.keyPress(KeyEvent.VK_M); // Launch "One" action
+            robo.keyRelease(KeyEvent.VK_M);
+
+            robo.waitForIdle();
+
+            if (!passed) {
+                throw new RuntimeException("Test failed.");
             }
-        });
-
-        robo.waitForIdle();
-
-        int altKey = java.awt.event.KeyEvent.VK_ALT;
-        robo.setAutoDelay(100);
-        Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu
-        robo.keyPress(KeyEvent.VK_S);  // Enter submenu
-        robo.keyRelease(KeyEvent.VK_S);
-        robo.keyPress(KeyEvent.VK_O); // Launch "One" action
-        robo.keyRelease(KeyEvent.VK_O);
-        robo.keyPress(KeyEvent.VK_M); // Launch "One" action
-        robo.keyRelease(KeyEvent.VK_M);
-
-        robo.waitForIdle();
-
-        if (!passed) {
-            throw new RuntimeException("Test failed.");
+        } finally {
+             if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
         }
-
     }
 
     private static void createAndShowGUI() {
-        JFrame mainFrame = new JFrame("Bug 4692443");
+        mainFrame = new JFrame("Bug 4692443");
         JMenuBar mbar = new JMenuBar();
         JMenu menu = new JMenu("File");
         menu.setMnemonic('F');
diff --git a/test/jdk/javax/swing/JMenu/6359669/bug6359669.java b/test/jdk/javax/swing/JMenu/6359669/bug6359669.java
index 06f1286..e6bfeca 100644
--- a/test/jdk/javax/swing/JMenu/6359669/bug6359669.java
+++ b/test/jdk/javax/swing/JMenu/6359669/bug6359669.java
@@ -39,48 +39,52 @@
 
 public class bug6359669 {
     static JMenu menu;
+    static JFrame f;
 
     public static void main(String[] args) throws Exception {
+        try {
+            ExtendedRobot robot = new ExtendedRobot();
+            robot.setAutoDelay(10);
 
-        ExtendedRobot robot = new ExtendedRobot();
-        robot.setAutoDelay(10);
+            SwingUtilities.invokeLater(new Runnable() {
+                public void run() {
+                    f = new JFrame();
+                    JMenuBar menuBar = new JMenuBar();
+                    menu = new JMenu("Test");
+                    menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {
+                        public void popupMenuCanceled(PopupMenuEvent e) {
+                        }
 
-        SwingUtilities.invokeLater(new Runnable() {
-            public void run() {
-                JFrame f = new JFrame();
-                JMenuBar menuBar = new JMenuBar();
-                menu = new JMenu("Test");
-                menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {
-                    public void popupMenuCanceled(PopupMenuEvent e) {
-                    }
+                        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
+                        }
 
-                    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
-                    }
+                        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
+                            menu.add(new JMenuItem("An item"));
+                        }
+                    });
 
-                    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
-                        menu.add(new JMenuItem("An item"));
-                    }
-                });
+                    menuBar.add(menu);
+                    f.setJMenuBar(menuBar);
 
-                menuBar.add(menu);
-                f.setJMenuBar(menuBar);
-
-                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                f.setSize(200, 200);
-                f.setVisible(true);
+                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    f.setSize(200, 200);
+                    f.setVisible(true);
+                }
+            });
+            robot.waitForIdle();
+            Point p = menu.getLocationOnScreen();
+            Dimension size = menu.getSize();
+            p.x += size.width / 2;
+            p.y += size.height / 2;
+            robot.mouseMove(p.x, p.y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+            if (menu.getPopupMenu().getComponentCount() == 0) {
+                throw new RuntimeException("Where is a menuItem ?");
             }
-        });
-        robot.waitForIdle();
-        Point p = menu.getLocationOnScreen();
-        Dimension size = menu.getSize();
-        p.x += size.width / 2;
-        p.y += size.height / 2;
-        robot.mouseMove(p.x, p.y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-        if (menu.getPopupMenu().getComponentCount() == 0) {
-            throw new RuntimeException("Where is a menuItem ?");
+        } finally {
+            if (f != null) SwingUtilities.invokeAndWait(() -> f.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JMenu/6470128/bug6470128.java b/test/jdk/javax/swing/JMenu/6470128/bug6470128.java
index 534cf5e..ec02975 100644
--- a/test/jdk/javax/swing/JMenu/6470128/bug6470128.java
+++ b/test/jdk/javax/swing/JMenu/6470128/bug6470128.java
@@ -43,47 +43,51 @@
     static JMenu subMenu;
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame();
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-                JMenuBar bar = new JMenuBar();
-                JMenu menu = new JMenu("Menu");
-                menu.setMnemonic('m');
-                subMenu = new JMenu("SubMenu");
-                JMenuItem item = new JMenuItem("Item");
+                    JMenuBar bar = new JMenuBar();
+                    JMenu menu = new JMenu("Menu");
+                    menu.setMnemonic('m');
+                    subMenu = new JMenu("SubMenu");
+                    JMenuItem item = new JMenuItem("Item");
 
-                frame.setJMenuBar(bar);
-                bar.add(menu);
-                menu.add(subMenu);
-                subMenu.add(item);
+                    frame.setJMenuBar(bar);
+                    bar.add(menu);
+                    menu.add(subMenu);
+                    subMenu.add(item);
 
-                frame.setSize(200, 200);
-                frame.setLocationRelativeTo(null);
-                frame.setVisible(true);
+                    frame.setSize(200, 200);
+                    frame.setLocationRelativeTo(null);
+                    frame.setVisible(true);
+                }
+            });
+            Robot robot = new Robot();
+            robot.setAutoDelay(10);
+            robot.waitForIdle();
+            if (Platform.isOSX()) {
+                robot.keyPress(KeyEvent.VK_CONTROL);
             }
-        });
-        Robot robot = new Robot();
-        robot.setAutoDelay(10);
-        robot.waitForIdle();
-        if (Platform.isOSX()) {
-            robot.keyPress(KeyEvent.VK_CONTROL);
-        }
-        robot.keyPress(KeyEvent.VK_ALT);
-        robot.keyPress(KeyEvent.VK_M);
-        robot.keyRelease(KeyEvent.VK_M);
-        robot.keyRelease(KeyEvent.VK_ALT);
-        if (Platform.isOSX()) {
-            robot.keyRelease(KeyEvent.VK_CONTROL);
-        }
-        robot.keyPress(KeyEvent.VK_ENTER);
-        robot.keyRelease(KeyEvent.VK_ENTER);
-        robot.keyPress(KeyEvent.VK_ESCAPE);
-        robot.keyRelease(KeyEvent.VK_ESCAPE);
-        robot.waitForIdle();
-        if (!subMenu.isSelected()) {
-            throw new RuntimeException("Submenu is unexpectedly unselected");
+            robot.keyPress(KeyEvent.VK_ALT);
+            robot.keyPress(KeyEvent.VK_M);
+            robot.keyRelease(KeyEvent.VK_M);
+            robot.keyRelease(KeyEvent.VK_ALT);
+            if (Platform.isOSX()) {
+                robot.keyRelease(KeyEvent.VK_CONTROL);
+            }
+            robot.keyPress(KeyEvent.VK_ENTER);
+            robot.keyRelease(KeyEvent.VK_ENTER);
+            robot.keyPress(KeyEvent.VK_ESCAPE);
+            robot.keyRelease(KeyEvent.VK_ESCAPE);
+            robot.waitForIdle();
+            if (!subMenu.isSelected()) {
+                throw new RuntimeException("Submenu is unexpectedly unselected");
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JMenu/6538132/bug6538132.java b/test/jdk/javax/swing/JMenu/6538132/bug6538132.java
index fa14979..23eec8d 100644
--- a/test/jdk/javax/swing/JMenu/6538132/bug6538132.java
+++ b/test/jdk/javax/swing/JMenu/6538132/bug6538132.java
@@ -42,6 +42,7 @@
     private static JMenu menu1;
     private static JMenu menu2;
     private static volatile boolean isWinLaf;
+    private static JFrame frame;
 
     private static void createGui() {
         try {
@@ -53,7 +54,7 @@
             isWinLaf = false;
             return;
         }
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         JMenuBar menuBar = new JMenuBar();
@@ -76,35 +77,39 @@
     }
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                bug6538132.createGui();
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    bug6538132.createGui();
+                }
+            });
+            if(isWinLaf) {
+                ExtendedRobot robot = new ExtendedRobot();
+                robot.setAutoDelay(10);
+                robot.waitForIdle();
+                Point p1 = menu1.getLocationOnScreen();
+                final int x1 = p1.x + menu1.getWidth() / 2;
+                final int y1 = p1.y + menu1.getHeight() / 2;
+                robot.glide(0, 0, x1, y1);
+                robot.mousePress(InputEvent.BUTTON1_MASK);
+                robot.mouseRelease(InputEvent.BUTTON1_MASK);
+                assertPopupOpen();
+                Point p2 = menu2.getLocationOnScreen();
+                final int x2 = p2.x + menu2.getWidth() / 2;
+                final int y2 = p2.y + menu2.getHeight() / 2;
+                robot.glide(x1, y1, x2, y2);
+                assertPopupOpen();
+                robot.keyPress(KeyEvent.VK_ESCAPE);
+                robot.keyRelease(KeyEvent.VK_ESCAPE);
+                assertPopupNotOpen();
+                robot.glide(x2, y2, x1, y1);
+                assertPopupNotOpen();
+                robot.mousePress(InputEvent.BUTTON1_MASK);
+                robot.mouseRelease(InputEvent.BUTTON1_MASK);
+                assertPopupOpen();
             }
-        });
-        if(isWinLaf) {
-            ExtendedRobot robot = new ExtendedRobot();
-            robot.setAutoDelay(10);
-            robot.waitForIdle();
-            Point p1 = menu1.getLocationOnScreen();
-            final int x1 = p1.x + menu1.getWidth() / 2;
-            final int y1 = p1.y + menu1.getHeight() / 2;
-            robot.glide(0, 0, x1, y1);
-            robot.mousePress(InputEvent.BUTTON1_MASK);
-            robot.mouseRelease(InputEvent.BUTTON1_MASK);
-            assertPopupOpen();
-            Point p2 = menu2.getLocationOnScreen();
-            final int x2 = p2.x + menu2.getWidth() / 2;
-            final int y2 = p2.y + menu2.getHeight() / 2;
-            robot.glide(x1, y1, x2, y2);
-            assertPopupOpen();
-            robot.keyPress(KeyEvent.VK_ESCAPE);
-            robot.keyRelease(KeyEvent.VK_ESCAPE);
-            assertPopupNotOpen();
-            robot.glide(x2, y2, x1, y1);
-            assertPopupNotOpen();
-            robot.mousePress(InputEvent.BUTTON1_MASK);
-            robot.mouseRelease(InputEvent.BUTTON1_MASK);
-            assertPopupOpen();
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JMenuBar/4750590/bug4750590.java b/test/jdk/javax/swing/JMenuBar/4750590/bug4750590.java
index d2d7019..ac2044e 100644
--- a/test/jdk/javax/swing/JMenuBar/4750590/bug4750590.java
+++ b/test/jdk/javax/swing/JMenuBar/4750590/bug4750590.java
@@ -40,35 +40,39 @@
 
     public static PassedListener pass = new PassedListener();
     public static volatile boolean passed = false;
+    private static JFrame mainFrame;
 
     public static void main(String args[]) throws Throwable {
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            @Override
-            public void run() {
-                createAndShowGUI();
+            Robot robo = new Robot();
+            robo.setAutoDelay(500);
+            robo.waitForIdle();
+
+            Util.hitMnemonics(robo, KeyEvent.VK_F);
+            robo.keyPress(KeyEvent.VK_M);
+            robo.keyRelease(KeyEvent.VK_M);
+
+            robo.waitForIdle();
+
+            if (passed) {
+                System.out.println("Test passed!");
+            } else {
+                throw new RuntimeException("Test FAILED!");
             }
-        });
-
-        Robot robo = new Robot();
-        robo.setAutoDelay(500);
-        robo.waitForIdle();
-
-        Util.hitMnemonics(robo, KeyEvent.VK_F);
-        robo.keyPress(KeyEvent.VK_M);
-        robo.keyRelease(KeyEvent.VK_M);
-
-        robo.waitForIdle();
-
-        if (passed) {
-            System.out.println("Test passed!");
-        } else {
-            throw new RuntimeException("Test FAILED!");
+        } finally {
+            if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
         }
     }
 
     private static void createAndShowGUI() {
-        JFrame mainFrame = new JFrame("Bug 4750590");
+        mainFrame = new JFrame("Bug 4750590");
         JMenuBar mbar = new JMenuBar();
         JMenu menu = new JMenu("File");
         menu.setMnemonic('F');
diff --git a/test/jdk/javax/swing/JMenuItem/4171437/bug4171437.java b/test/jdk/javax/swing/JMenuItem/4171437/bug4171437.java
index 43914ca..3bac95f 100644
--- a/test/jdk/javax/swing/JMenuItem/4171437/bug4171437.java
+++ b/test/jdk/javax/swing/JMenuItem/4171437/bug4171437.java
@@ -40,26 +40,31 @@
 public class bug4171437 {
     static volatile boolean closeActivated = false;
     static volatile boolean customActivated = false;
+    static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
+            robot.waitForIdle();
+
+            Util.hitMnemonics(robot, KeyEvent.VK_F);
+            Util.hitKeys(robot, KeyEvent.VK_C);
+
+            robot.waitForIdle();
+            Thread.sleep(1000);
+
+            if (!closeActivated || customActivated) {
+                throw new RuntimeException("Didn't pass the muster");
             }
-        });
-
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
-        robot.waitForIdle();
-
-        Util.hitMnemonics(robot, KeyEvent.VK_F);
-        Util.hitKeys(robot, KeyEvent.VK_C);
-
-        robot.waitForIdle();
-        Thread.sleep(1000);
-
-        if (!closeActivated || customActivated) {
-            throw new RuntimeException("Didn't pass the muster");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
     public static void createAndShowGUI() {
@@ -99,7 +104,7 @@
         });
         menubar.add( custom);
 
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setJMenuBar( menubar);
         frame.setSize(300, 300);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
diff --git a/test/jdk/javax/swing/JMenuItem/4654927/bug4654927.java b/test/jdk/javax/swing/JMenuItem/4654927/bug4654927.java
index 8a4384e4..b1a757e 100644
--- a/test/jdk/javax/swing/JMenuItem/4654927/bug4654927.java
+++ b/test/jdk/javax/swing/JMenuItem/4654927/bug4654927.java
@@ -42,79 +42,84 @@
 
     private static volatile JMenu menu;
     private static volatile JMenuItem menuItem;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        String systemLAF = UIManager.getSystemLookAndFeelClassName();
-        // the test is not applicable to Motif L&F
-        if(systemLAF.endsWith("MotifLookAndFeel")){
-            return;
-        }
-
-        UIManager.setLookAndFeel(systemLAF);
-        Robot robot = new Robot();
-        robot.setAutoDelay(10);
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            public void run() {
-                createAndShowUI();
+        try {
+            String systemLAF = UIManager.getSystemLookAndFeelClassName();
+            // the test is not applicable to Motif L&F
+            if(systemLAF.endsWith("MotifLookAndFeel")){
+                return;
             }
-        });
-        robot.waitForIdle();
 
-        // test mouse press
-        Point point = Util.getCenterPoint(menu);
-        robot.mouseMove(point.x, point.y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
+            UIManager.setLookAndFeel(systemLAF);
+            Robot robot = new Robot();
+            robot.setAutoDelay(10);
 
-        point = Util.getCenterPoint(menuItem);
-        robot.mouseMove(point.x, point.y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-        if (!isMenuItemShowing()) {
-            throw new RuntimeException("Popup is unexpectedly closed");
-        }
+                public void run() {
+                    createAndShowUI();
+                }
+            });
+            robot.waitForIdle();
 
-        // test mouse drag
-        point = Util.getCenterPoint(menu);
-        robot.mouseMove(point.x, point.y);
-        Point menuLocation = Util.invokeOnEDT(new Callable<Point>() {
+            // test mouse press
+            Point point = Util.getCenterPoint(menu);
+            robot.mouseMove(point.x, point.y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
 
-            @Override
-            public Point call() throws Exception {
-                return menu.getLocationOnScreen();
+            point = Util.getCenterPoint(menuItem);
+            robot.mouseMove(point.x, point.y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            if (!isMenuItemShowing()) {
+                throw new RuntimeException("Popup is unexpectedly closed");
             }
-        });
 
-        Point itemLocation = Util.invokeOnEDT(new Callable<Point>() {
+            // test mouse drag
+            point = Util.getCenterPoint(menu);
+            robot.mouseMove(point.x, point.y);
+            Point menuLocation = Util.invokeOnEDT(new Callable<Point>() {
 
-            @Override
-            public Point call() throws Exception {
-                return menuItem.getLocationOnScreen();
+                @Override
+                public Point call() throws Exception {
+                    return menu.getLocationOnScreen();
+                }
+            });
+
+            Point itemLocation = Util.invokeOnEDT(new Callable<Point>() {
+
+                @Override
+                public Point call() throws Exception {
+                    return menuItem.getLocationOnScreen();
+                }
+            });
+
+            int x0 = menuLocation.x + 10;
+            int y0 = menuLocation.y + 10;
+            int x1 = itemLocation.x + 10;
+            int y1 = itemLocation.y + 10;
+
+            // close menu
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            Util.glide(robot, x0, y0, x1, y1);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            if (!isMenuItemShowing()) {
+                throw new RuntimeException("Popup is unexpectedly closed");
             }
-        });
-
-        int x0 = menuLocation.x + 10;
-        int y0 = menuLocation.y + 10;
-        int x1 = itemLocation.x + 10;
-        int y1 = itemLocation.y + 10;
-
-        // close menu
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        Util.glide(robot, x0, y0, x1, y1);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-
-        if (!isMenuItemShowing()) {
-            throw new RuntimeException("Popup is unexpectedly closed");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
@@ -129,7 +134,7 @@
     }
 
     private static void createAndShowUI() {
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         menu = new JMenu("Menu");
diff --git a/test/jdk/javax/swing/JMenuItem/6209975/bug6209975.java b/test/jdk/javax/swing/JMenuItem/6209975/bug6209975.java
index 00d0e88..c1ef0fe 100644
--- a/test/jdk/javax/swing/JMenuItem/6209975/bug6209975.java
+++ b/test/jdk/javax/swing/JMenuItem/6209975/bug6209975.java
@@ -40,37 +40,41 @@
 
     private static JMenu menu;
     private static JButton button;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-
-        Robot robot = new Robot();
-        robot.setAutoDelay(500);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(500);
 
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                createAndShowGUI();
+                @Override
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+
+            robot.waitForIdle();
+
+            Point clickPoint = getButtonClickPoint();
+            robot.mouseMove(clickPoint.x, clickPoint.y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            clickPoint = getMenuClickPoint();
+            robot.mouseMove(clickPoint.x, clickPoint.y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            if (RO1.itsValue <= RO2.itsValue) {
+                throw new RuntimeException("Offset if the second icon is invalid.");
             }
-        });
-
-        robot.waitForIdle();
-
-        Point clickPoint = getButtonClickPoint();
-        robot.mouseMove(clickPoint.x, clickPoint.y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-
-        clickPoint = getMenuClickPoint();
-        robot.mouseMove(clickPoint.x, clickPoint.y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-
-        if (RO1.itsValue <= RO2.itsValue) {
-            throw new RuntimeException("Offset if the second icon is invalid.");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
@@ -105,7 +109,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame("Test6209975");
+        frame = new JFrame("Test6209975");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
         frame.setLayout(new BorderLayout());
diff --git a/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java b/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
index fa2a8e8..a66ba00 100644
--- a/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
+++ b/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
@@ -40,7 +40,7 @@
     public boolean passed = true;
     public boolean done = false;
 
-    public JFrame mainFrame = new JFrame("Bug4634626");
+    public static JFrame mainFrame = new JFrame("Bug4634626");
     public JRootPane rootPane = mainFrame.getRootPane();
     public JPanel contentPane = new JPanel();
     public JButton nopButton = new JButton("No popup button");
@@ -83,10 +83,14 @@
 
     public static MouseWatcher mouser = new MouseWatcher();
 
-    public static void main(final String[] args) {
-        bug4634626 app = new bug4634626();
-        app.init();
-        app.destroy();
+    public static void main(final String[] args) throws Exception {
+        try {
+            bug4634626 app = new bug4634626();
+            app.init();
+            app.destroy();
+        } finally {
+            if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
+        }
     }
 
     public void init() {
diff --git a/test/jdk/javax/swing/JPopupMenu/6217905/bug6217905.java b/test/jdk/javax/swing/JPopupMenu/6217905/bug6217905.java
index 8af3684..a79f96c 100644
--- a/test/jdk/javax/swing/JPopupMenu/6217905/bug6217905.java
+++ b/test/jdk/javax/swing/JPopupMenu/6217905/bug6217905.java
@@ -42,9 +42,10 @@
     private static JPanel popupPanel;
     private static JMenuItem firstItem;
     private static JMenuItem lastItem;
+    private static JFrame frame;
 
     private static void createGui() {
-        final JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         JPopupMenu popup = new JPopupMenu("Menu");
@@ -70,44 +71,48 @@
             return;
         }
 
-        ExtendedRobot robot = new ExtendedRobot();
-        robot.setAutoDelay(10);
-        SwingUtilities.invokeLater(new Runnable() {
-            public void run() {
-                bug6217905.createGui();
+        try {
+            ExtendedRobot robot = new ExtendedRobot();
+            robot.setAutoDelay(10);
+            SwingUtilities.invokeLater(new Runnable() {
+                public void run() {
+                    bug6217905.createGui();
+                }
+            });
+            robot.waitForIdle();
+            Point loc = popupPanel.getLocationOnScreen();
+            int x = loc.x + popupPanel.getWidth()/2;
+            int y = loc.y + popupPanel.getHeight()/2;
+            robot.glide(0, 0, x, y);
+            robot.mousePress(InputEvent.BUTTON3_MASK);
+            robot.mouseRelease(InputEvent.BUTTON3_MASK);
+            robot.waitForIdle();
+            if (getSelectedPathLength() != 1) {
+                throw new RuntimeException("Only popup must be selected");
             }
-        });
-        robot.waitForIdle();
-        Point loc = popupPanel.getLocationOnScreen();
-        int x = loc.x + popupPanel.getWidth()/2;
-        int y = loc.y + popupPanel.getHeight()/2;
-        robot.glide(0, 0, x, y);
-        robot.mousePress(InputEvent.BUTTON3_MASK);
-        robot.mouseRelease(InputEvent.BUTTON3_MASK);
-        robot.waitForIdle();
-        if (getSelectedPathLength() != 1) {
-            throw new RuntimeException("Only popup must be selected");
-        }
-        robot.glide(x, y, 0, 0);
-        robot.type(KeyEvent.VK_DOWN);
-        robot.waitForIdle();
-        if (getSelectedPathLength() != 2 || !firstItem.isArmed()) {
-            throw new RuntimeException("First item must be selected");
-        }
-        robot.type(KeyEvent.VK_ESCAPE);
-        robot.waitForIdle();
-        if (getSelectedPathLength() != 0) {
-            throw new RuntimeException("There must be no selected items");
-        }
-        robot.glide(0, 0, x, y);
-        robot.mousePress(InputEvent.BUTTON3_MASK);
-        robot.mouseRelease(InputEvent.BUTTON3_MASK);
-        robot.waitForIdle();
-        robot.glide(x, y, 0, 0);
-        robot.type(KeyEvent.VK_UP);
-        robot.waitForIdle();
-        if (getSelectedPathLength() != 2 || !lastItem.isArmed()) {
-            throw new RuntimeException("Last item must be selected");
+            robot.glide(x, y, 0, 0);
+            robot.type(KeyEvent.VK_DOWN);
+            robot.waitForIdle();
+            if (getSelectedPathLength() != 2 || !firstItem.isArmed()) {
+                throw new RuntimeException("First item must be selected");
+            }
+            robot.type(KeyEvent.VK_ESCAPE);
+            robot.waitForIdle();
+            if (getSelectedPathLength() != 0) {
+                throw new RuntimeException("There must be no selected items");
+            }
+            robot.glide(0, 0, x, y);
+            robot.mousePress(InputEvent.BUTTON3_MASK);
+            robot.mouseRelease(InputEvent.BUTTON3_MASK);
+            robot.waitForIdle();
+            robot.glide(x, y, 0, 0);
+            robot.type(KeyEvent.VK_UP);
+            robot.waitForIdle();
+            if (getSelectedPathLength() != 2 || !lastItem.isArmed()) {
+                throw new RuntimeException("Last item must be selected");
+            }
+        } finally {
+             if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JPopupMenu/6515446/bug6515446.java b/test/jdk/javax/swing/JPopupMenu/6515446/bug6515446.java
index a29d0b1..0ee6ff2 100644
--- a/test/jdk/javax/swing/JPopupMenu/6515446/bug6515446.java
+++ b/test/jdk/javax/swing/JPopupMenu/6515446/bug6515446.java
@@ -38,55 +38,60 @@
 public class bug6515446 {
     private static JPanel panel;
     private static volatile boolean flag;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeLater(new Runnable() {
-            public void run() {
-                final JFrame frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        try {
+            SwingUtilities.invokeLater(new Runnable() {
+                public void run() {
+                    frame = new JFrame();
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-                final JPopupMenu popup = new JPopupMenu("Menu");
-                JMenuItem item = new JMenuItem("MenuItem");
-                item.addActionListener(new ActionListener() {
-                    public void actionPerformed(ActionEvent e) {
-                        flag = true;
-                    }
-                });
-                popup.add(item);
+                    final JPopupMenu popup = new JPopupMenu("Menu");
+                    JMenuItem item = new JMenuItem("MenuItem");
+                    item.addActionListener(new ActionListener() {
+                        public void actionPerformed(ActionEvent e) {
+                            flag = true;
+                        }
+                    });
+                    popup.add(item);
 
-                panel = new JPanel();
-                panel.addMouseListener(new MouseAdapter() {
-                    public void mousePressed(MouseEvent e) {
-                        popup.show(panel, e.getX(), e.getY());
-                    }
+                    panel = new JPanel();
+                    panel.addMouseListener(new MouseAdapter() {
+                        public void mousePressed(MouseEvent e) {
+                            popup.show(panel, e.getX(), e.getY());
+                        }
 
-                    public void mouseReleased(MouseEvent e) {
-                        popup.setVisible(false);
-                    }
-                });
-                frame.add(panel);
-                frame.setSize(200, 200);
-                frame.setLocationRelativeTo(null);
-                frame.setVisible(true);
+                        public void mouseReleased(MouseEvent e) {
+                            popup.setVisible(false);
+                        }
+                    });
+                    frame.add(panel);
+                    frame.setSize(200, 200);
+                    frame.setLocationRelativeTo(null);
+                    frame.setVisible(true);
+                }
+            });
+
+            ExtendedRobot robot = new ExtendedRobot();
+            robot.setAutoDelay(10);
+            robot.waitForIdle();
+
+            Point l = panel.getLocationOnScreen();
+
+            int x = l.x + panel.getWidth() / 2;
+            int y = l.y + panel.getHeight() / 2;
+            robot.mouseMove(x, y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.glide(x, y, x + 10, y + 10);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            if (!flag) {
+                throw new RuntimeException("ActionEvent wasn't fired");
             }
-        });
-
-        ExtendedRobot robot = new ExtendedRobot();
-        robot.setAutoDelay(10);
-        robot.waitForIdle();
-
-        Point l = panel.getLocationOnScreen();
-
-        int x = l.x + panel.getWidth() / 2;
-        int y = l.y + panel.getHeight() / 2;
-        robot.mouseMove(x, y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.glide(x, y, x + 10, y + 10);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-
-        if (!flag) {
-            throw new RuntimeException("ActionEvent wasn't fired");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JPopupMenu/6580930/bug6580930.java b/test/jdk/javax/swing/JPopupMenu/6580930/bug6580930.java
index 6e935e7..a3537af 100644
--- a/test/jdk/javax/swing/JPopupMenu/6580930/bug6580930.java
+++ b/test/jdk/javax/swing/JPopupMenu/6580930/bug6580930.java
@@ -63,69 +63,73 @@
 
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                JPopupMenu.setDefaultLightWeightPopupEnabled(true);
-                bug6580930.createGui();
-            }
-        });
-
-        toolkit = Toolkit.getDefaultToolkit();
-        robot = new ExtendedRobot();
-        robot.setAutoDelay(10);
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                Insets insets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());
-                if (insets.bottom == 0) {
-                    System.out.println("This test is only for configurations with taskbar on the bottom");
-
-                    skipTest = true;
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    JPopupMenu.setDefaultLightWeightPopupEnabled(true);
+                    bug6580930.createGui();
                 }
+            });
 
-                Dimension screenSize = toolkit.getScreenSize();
-                frame.setLocation(screenSize.width/2, screenSize.height - frame.getHeight() - insets.bottom + 10);
-                frame.setVisible(true);
+            toolkit = Toolkit.getDefaultToolkit();
+            robot = new ExtendedRobot();
+            robot.setAutoDelay(10);
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    Insets insets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());
+                    if (insets.bottom == 0) {
+                        System.out.println("This test is only for configurations with taskbar on the bottom");
+
+                        skipTest = true;
+                    }
+
+                    Dimension screenSize = toolkit.getScreenSize();
+                    frame.setLocation(screenSize.width/2, screenSize.height - frame.getHeight() - insets.bottom + 10);
+                    frame.setVisible(true);
+                }
+            });
+
+            robot.waitForIdle();
+
+            if(skipTest) {
+                return;
             }
-        });
 
-        robot.waitForIdle();
+            SwingUtilities.invokeAndWait(() -> loc = frame.getLocationOnScreen());
+            robot.waitForIdle();
 
-        if(skipTest) {
-            return;
-        }
+            robot.mouseMove(loc.x, loc.y);
+            showPopup();
+            robot.waitForIdle();
+            if (!System.getProperty("os.name").startsWith("Mac")
+                && isHeavyWeightMenuVisible()) {
+                throw new RuntimeException("HeavyWeightPopup is unexpectedly visible");
+            }
 
-        SwingUtilities.invokeAndWait(() -> loc = frame.getLocationOnScreen());
-        robot.waitForIdle();
+            robot.keyPress(KeyEvent.VK_ESCAPE);
+            robot.keyRelease(KeyEvent.VK_ESCAPE);
 
-        robot.mouseMove(loc.x, loc.y);
-        showPopup();
-        robot.waitForIdle();
-        if (!System.getProperty("os.name").startsWith("Mac")
-            && isHeavyWeightMenuVisible()) {
-            throw new RuntimeException("HeavyWeightPopup is unexpectedly visible");
-        }
+            int x = loc.x;
+            SwingUtilities.invokeAndWait( () -> y = loc.y + (frame.getHeight() -
+                    popup.getPreferredSize().height) + 1);
+            robot.waitForIdle();
+            robot.mouseMove(x, y);
 
-        robot.keyPress(KeyEvent.VK_ESCAPE);
-        robot.keyRelease(KeyEvent.VK_ESCAPE);
+            showPopup();
+            SwingUtilities.invokeAndWait(() -> loc = popup.getLocationOnScreen());
+            robot.waitForIdle();
 
-        int x = loc.x;
-        SwingUtilities.invokeAndWait( () -> y = loc.y + (frame.getHeight() -
-                popup.getPreferredSize().height) + 1);
-        robot.waitForIdle();
-        robot.mouseMove(x, y);
+            if (!loc.equals(new Point(x, y))) {
+                throw new RuntimeException("Popup is unexpectedly shifted");
+            }
 
-        showPopup();
-        SwingUtilities.invokeAndWait(() -> loc = popup.getLocationOnScreen());
-        robot.waitForIdle();
-
-        if (!loc.equals(new Point(x, y))) {
-            throw new RuntimeException("Popup is unexpectedly shifted");
-        }
-
-        if (!isHeavyWeightMenuVisible()) {
-            throw new RuntimeException("HeavyWeightPopup is unexpectedly hidden");
+            if (!isHeavyWeightMenuVisible()) {
+                throw new RuntimeException("HeavyWeightPopup is unexpectedly hidden");
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JPopupMenu/6827786/bug6827786.java b/test/jdk/javax/swing/JPopupMenu/6827786/bug6827786.java
index 794f2743a..7d046b6 100644
--- a/test/jdk/javax/swing/JPopupMenu/6827786/bug6827786.java
+++ b/test/jdk/javax/swing/JPopupMenu/6827786/bug6827786.java
@@ -44,56 +44,60 @@
 
     private static JMenu menu;
     private static Component focusable;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
-        // move mouse outside menu to prevent auto selection
-        robot.mouseMove(100,100);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
+            // move mouse outside menu to prevent auto selection
+           robot.mouseMove(100,100);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            public void run() {
-                createAndShowGUI();
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                public void run() {
+                    focusable.requestFocus();
+                }
+            });
+
+            robot.waitForIdle();
+            checkfocus();
+
+            // select menu
+            if (Platform.isOSX()) {
+                Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_F);
+            } else {
+                Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_F);
             }
-        });
+            // select submenu
+            Util.hitKeys(robot, KeyEvent.VK_S);
+            robot.waitForIdle();
+            // verify submenu is selected
+            verify(1);
 
-        robot.waitForIdle();
+            Util.hitKeys(robot, KeyEvent.VK_S);
+            robot.waitForIdle();
+            // verify last item is selected
+            verify(2);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            Util.hitKeys(robot, KeyEvent.VK_S);
+            robot.waitForIdle();
+            // selection should wrap to first item
+            verify(0);
 
-            public void run() {
-                focusable.requestFocus();
-            }
-        });
-
-        robot.waitForIdle();
-        checkfocus();
-
-        // select menu
-        if (Platform.isOSX()) {
-            Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_F);
-        } else {
-            Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_F);
+            System.out.println("PASSED");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
-        // select submenu
-        Util.hitKeys(robot, KeyEvent.VK_S);
-        robot.waitForIdle();
-        // verify submenu is selected
-        verify(1);
-
-        Util.hitKeys(robot, KeyEvent.VK_S);
-        robot.waitForIdle();
-        // verify last item is selected
-        verify(2);
-
-        Util.hitKeys(robot, KeyEvent.VK_S);
-        robot.waitForIdle();
-        // selection should wrap to first item
-        verify(0);
-
-        System.out.println("PASSED");
-
     }
 
     private static void checkfocus() throws Exception {
@@ -144,7 +148,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame("bug6827786");
+        frame = new JFrame("bug6827786");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setJMenuBar(createMenuBar());
         focusable = new JButton("Set Focus Here");
diff --git a/test/jdk/javax/swing/JPopupMenu/6987844/bug6987844.java b/test/jdk/javax/swing/JPopupMenu/6987844/bug6987844.java
index 97790ca..061aae5 100644
--- a/test/jdk/javax/swing/JPopupMenu/6987844/bug6987844.java
+++ b/test/jdk/javax/swing/JPopupMenu/6987844/bug6987844.java
@@ -37,56 +37,61 @@
 public class bug6987844 {
     static JMenu menu1;
     static JMenu menu2;
+    static JFrame frame;
 
     public static void main(String... args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(200);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(200);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                JFrame frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame();
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-                JMenuBar bar = new JMenuBar();
-                menu1 = new JMenu("Menu1");
-                menu1.add(new JMenuItem("item"));
-                bar.add(menu1);
-                menu2 = new JMenu("Menu2");
-                menu2.add(new JMenuItem("item"));
-                menu2.add(new JMenuItem("item"));
-                bar.add(menu2);
+                    JMenuBar bar = new JMenuBar();
+                    menu1 = new JMenu("Menu1");
+                    menu1.add(new JMenuItem("item"));
+                    bar.add(menu1);
+                    menu2 = new JMenu("Menu2");
+                    menu2.add(new JMenuItem("item"));
+                    menu2.add(new JMenuItem("item"));
+                    bar.add(menu2);
 
-                frame.setJMenuBar(bar);
-                frame.pack();
+                    frame.setJMenuBar(bar);
+                    frame.pack();
 
-                frame.setVisible(true);
-            }
-        });
-        robot.waitForIdle();
-        Point point1 = menu1.getLocationOnScreen();
-        Point point2 = menu2.getLocationOnScreen();
-
-        robot.mouseMove(point1.x + 1, point1.y + 1);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-
-        robot.mouseMove(point2.x + 1, point2.y + 1);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-
-        robot.mouseMove(point1.x + 1, point1.y + 1);
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                Dimension popupSize1 = menu1.getPopupMenu().getSize();
-                Dimension popupSize2 = menu2.getPopupMenu().getSize();
-                if (popupSize1.equals(popupSize2)) {
-                    throw new RuntimeException("First popup unexpedetly changed its size");
+                    frame.setVisible(true);
                 }
-            }
-        });
+            });
+            robot.waitForIdle();
+            Point point1 = menu1.getLocationOnScreen();
+            Point point2 = menu2.getLocationOnScreen();
+
+            robot.mouseMove(point1.x + 1, point1.y + 1);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+
+            robot.mouseMove(point2.x + 1, point2.y + 1);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+
+            robot.mouseMove(point1.x + 1, point1.y + 1);
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    Dimension popupSize1 = menu1.getPopupMenu().getSize();
+                    Dimension popupSize2 = menu2.getPopupMenu().getSize();
+                    if (popupSize1.equals(popupSize2)) {
+                        throw new RuntimeException("First popup unexpedetly changed its size");
+                    }
+                }
+            });
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 }
diff --git a/test/jdk/javax/swing/JRadioButton/8041561/bug8041561.java b/test/jdk/javax/swing/JRadioButton/8041561/bug8041561.java
index 12efa8b..de6e871 100644
--- a/test/jdk/javax/swing/JRadioButton/8041561/bug8041561.java
+++ b/test/jdk/javax/swing/JRadioButton/8041561/bug8041561.java
@@ -45,49 +45,53 @@
 public class bug8041561 {
 
     private static JRadioButton radioButton;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        SwingUtilities.invokeAndWait(new Runnable() {
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                try {
-                    MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
-                    UIManager.setLookAndFeel(new MetalLookAndFeel());
-                    createAndShowGUI();
-                } catch (UnsupportedLookAndFeelException e) {
-                    throw new RuntimeException(e);
-                }
-            }
-        });
-
-        new Robot().waitForIdle();
-        Thread.sleep(500);
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                try {
-                    Point point = radioButton.getLocationOnScreen();
-                    int x = (int) point.getX() + radioButton.getWidth() / 2;
-                    int y = (int) point.getY() + radioButton.getHeight() / 2;
-
-                    Robot robot = new Robot();
-                    Color color = robot.getPixelColor(x, y);
-                    if (!Color.BLUE.equals(color)) {
-                        throw new RuntimeException("JRadioButton is opaque");
+                @Override
+                public void run() {
+                    try {
+                        MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
+                        UIManager.setLookAndFeel(new MetalLookAndFeel());
+                        createAndShowGUI();
+                    } catch (UnsupportedLookAndFeelException e) {
+                        throw new RuntimeException(e);
                     }
-                } catch (AWTException e) {
-                    throw new RuntimeException(e);
                 }
-            }
-        });
+            });
 
+            new Robot().waitForIdle();
+            Thread.sleep(500);
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    try {
+                        Point point = radioButton.getLocationOnScreen();
+                        int x = (int) point.getX() + radioButton.getWidth() / 2;
+                        int y = (int) point.getY() + radioButton.getHeight() / 2;
+
+                        Robot robot = new Robot();
+                        Color color = robot.getPixelColor(x, y);
+                        if (!Color.BLUE.equals(color)) {
+                            throw new RuntimeException("JRadioButton is opaque");
+                        }
+                    } catch (AWTException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+            });
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setBackground(Color.BLUE);
         radioButton = new JRadioButton();
diff --git a/test/jdk/javax/swing/JRadioButton/8075609/bug8075609.java b/test/jdk/javax/swing/JRadioButton/8075609/bug8075609.java
index 664f5b4..31c9920 100644
--- a/test/jdk/javax/swing/JRadioButton/8075609/bug8075609.java
+++ b/test/jdk/javax/swing/JRadioButton/8075609/bug8075609.java
@@ -40,25 +40,30 @@
 public class bug8075609 {
     private static Robot robot;
     private static JTextField textField;
+    private static JFrame mainFrame;
 
     public static void main(String args[]) throws Throwable {
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
-            }
-        });
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-        robot = new Robot();
-        Thread.sleep(100);
+            robot = new Robot();
+            Thread.sleep(100);
 
-        robot.setAutoDelay(100);
+            robot.setAutoDelay(100);
 
-        // Radio button group tab key test
-        runTest1();
+            // Radio button group tab key test
+            runTest1();
+        } finally {
+            if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
+        }
     }
 
     private static void createAndShowGUI() {
-        JFrame mainFrame = new JFrame("Bug 8075609 - 1 test");
+        mainFrame = new JFrame("Bug 8075609 - 1 test");
 
         JPanel rootPanel = new JPanel();
         rootPanel.setLayout(new BorderLayout());
diff --git a/test/jdk/javax/swing/JRootPane/4670486/bug4670486.java b/test/jdk/javax/swing/JRootPane/4670486/bug4670486.java
index 150396e..6e1316a 100644
--- a/test/jdk/javax/swing/JRootPane/4670486/bug4670486.java
+++ b/test/jdk/javax/swing/JRootPane/4670486/bug4670486.java
@@ -39,6 +39,7 @@
 
     public static volatile boolean actionExpected = false;
     public static volatile boolean actionRecieved = false;
+    public static JFrame frame;
 
     private static JMenuBar createMenuBar() {
         JMenuBar menubar = new JMenuBar();
@@ -101,44 +102,48 @@
     }
 
     public static void main(String[] args) throws Throwable {
-        Robot robot = new Robot();
-        robot.setAutoDelay(250);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(250);
 
-        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                JFrame frame = new JFrame("Test");
-                frame.setContentPane(createPanel(frame));
-                frame.setJMenuBar(createMenuBar());
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                frame.pack();
-                frame.setVisible(true);
-            }
-        });
+                @Override
+                public void run() {
+                    frame = new JFrame("Test");
+                    frame.setContentPane(createPanel(frame));
+                    frame.setJMenuBar(createMenuBar());
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    frame.pack();
+                    frame.setVisible(true);
+                }
+            });
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        // Change the default button to
-        // force a call to BasicRootPaneUI.updateDefaultButtonBindings()
-        Util.hitKeys(robot, KeyEvent.VK_TAB);
+            // Change the default button to
+            // force a call to BasicRootPaneUI.updateDefaultButtonBindings()
+            Util.hitKeys(robot, KeyEvent.VK_TAB);
 
-        // If the bug exists, then as soon as the menu appears,
-        // the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no
-        // effect.
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-        Util.hitKeys(robot, KeyEvent.VK_ENTER);
-        robot.waitForIdle();
+            // If the bug exists, then as soon as the menu appears,
+            // the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no
+            // effect.
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
+            Util.hitKeys(robot, KeyEvent.VK_ENTER);
+            robot.waitForIdle();
 
-        checkAction();
+            checkAction();
 
-        Util.hitMnemonics(robot, KeyEvent.VK_U);
-        Util.hitKeys(robot, KeyEvent.VK_DOWN);
-        Util.hitKeys(robot, KeyEvent.VK_ENTER);
-        robot.waitForIdle();
+            Util.hitMnemonics(robot, KeyEvent.VK_U);
+            Util.hitKeys(robot, KeyEvent.VK_DOWN);
+            Util.hitKeys(robot, KeyEvent.VK_ENTER);
+            robot.waitForIdle();
 
-        checkAction();
+            checkAction();
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 }
diff --git a/test/jdk/javax/swing/JScrollBar/4708809/bug4708809.java b/test/jdk/javax/swing/JScrollBar/4708809/bug4708809.java
index 829df8d..13303bb 100644
--- a/test/jdk/javax/swing/JScrollBar/4708809/bug4708809.java
+++ b/test/jdk/javax/swing/JScrollBar/4708809/bug4708809.java
@@ -40,62 +40,67 @@
     private static volatile boolean passed = true;
     private static JScrollPane spane;
     private static JScrollBar sbar;
+    private static JFrame fr;
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(350);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(350);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            public void run() {
-                createAndShowGUI();
-            }
-        });
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            public void run() {
-                spane.requestFocus();
-                sbar.setValue(sbar.getMaximum());
-            }
-        });
+                public void run() {
+                    spane.requestFocus();
+                    sbar.setValue(sbar.getMaximum());
+                }
+            });
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        Point point = getClickPoint(0.5, 0.5);
-        robot.mouseMove(point.x, point.y);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
+            Point point = getClickPoint(0.5, 0.5);
+            robot.mouseMove(point.x, point.y);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            public void run() {
-                final int oldValue = sbar.getValue();
-                sbar.addAdjustmentListener(new AdjustmentListener() {
+                public void run() {
+                    final int oldValue = sbar.getValue();
+                    sbar.addAdjustmentListener(new AdjustmentListener() {
 
-                    public void adjustmentValueChanged(AdjustmentEvent e) {
-                        if (e.getValue() >= oldValue) {
-                            passed = false;
+                        public void adjustmentValueChanged(AdjustmentEvent e) {
+                            if (e.getValue() >= oldValue) {
+                                passed = false;
+                            }
+                            do_test = true;
                         }
-                        do_test = true;
-                    }
-                });
+                    });
 
+                }
+            });
+
+            robot.waitForIdle();
+
+            point = getClickPoint(0.5, 0.2);
+            robot.mouseMove(point.x, point.y);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            robot.waitForIdle();
+
+            if (!do_test || !passed) {
+                throw new Exception("The scrollbar moved with incorrect direction");
             }
-        });
-
-        robot.waitForIdle();
-
-        point = getClickPoint(0.5, 0.2);
-        robot.mouseMove(point.x, point.y);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-        robot.waitForIdle();
-
-        if (!do_test || !passed) {
-            throw new Exception("The scrollbar moved with incorrect direction");
+        } finally {
+            if (fr != null) SwingUtilities.invokeAndWait(() -> fr.dispose());
         }
 
     }
@@ -119,7 +124,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame fr = new JFrame("Test");
+        fr = new JFrame("Test");
 
         JLabel label = new JLabel("picture");
         label.setPreferredSize(new Dimension(500, 500));
diff --git a/test/jdk/javax/swing/JScrollBar/4865918/bug4865918.java b/test/jdk/javax/swing/JScrollBar/4865918/bug4865918.java
index d624923..eba58ef 100644
--- a/test/jdk/javax/swing/JScrollBar/4865918/bug4865918.java
+++ b/test/jdk/javax/swing/JScrollBar/4865918/bug4865918.java
@@ -38,33 +38,37 @@
 public class bug4865918 {
 
     private static TestScrollBar sbar;
+    private static JFrame frame;
 
     public static void main(String[] argv) throws Exception {
+        try {
+            Robot robot = new Robot();
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-        Robot robot = new Robot();
-        SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-            public void run() {
-                createAndShowGUI();
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    sbar.pressMouse();
+                }
+            });
+
+            robot.waitForIdle();
+
+            int value = getValue();
+
+            if (value != 9) {
+                throw new Error("The scrollbar block increment is incorect");
             }
-        });
-
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                sbar.pressMouse();
-            }
-        });
-
-        robot.waitForIdle();
-
-        int value = getValue();
-
-        if (value != 9) {
-            throw new Error("The scrollbar block increment is incorect");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
@@ -82,7 +86,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame("bug4865918");
+        frame = new JFrame("bug4865918");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         sbar = new TestScrollBar(JScrollBar.HORIZONTAL, -1, 10, -100, 100);
diff --git a/test/jdk/javax/swing/JScrollBar/6542335/bug6542335.java b/test/jdk/javax/swing/JScrollBar/6542335/bug6542335.java
index 8b44323..6cb0a2f 100644
--- a/test/jdk/javax/swing/JScrollBar/6542335/bug6542335.java
+++ b/test/jdk/javax/swing/JScrollBar/6542335/bug6542335.java
@@ -38,64 +38,69 @@
 public class bug6542335 {
     private static JScrollBar sb;
     private static MyScrollBarUI ui;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        final Robot robot = new Robot();
-        robot.setAutoDelay(10);
+        try {
+            final Robot robot = new Robot();
+            robot.setAutoDelay(10);
 
-        final Rectangle[] thumbBounds = new Rectangle[1];
+            final Rectangle[] thumbBounds = new Rectangle[1];
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                final JFrame frame = new JFrame("bug6542335");
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame("bug6542335");
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-                sb = new JScrollBar(0, 0, 1, 0, 1);
+                    sb = new JScrollBar(0, 0, 1, 0, 1);
 
-                ui = new MyScrollBarUI();
-                sb.setUI(ui);
+                    ui = new MyScrollBarUI();
+                    sb.setUI(ui);
 
-                sb.setPreferredSize(new Dimension(200, 17));
-                DefaultBoundedRangeModel rangeModel = new DefaultBoundedRangeModel();
-                rangeModel.setMaximum(100);
-                rangeModel.setMinimum(0);
-                rangeModel.setExtent(50);
-                rangeModel.setValue(50);
+                    sb.setPreferredSize(new Dimension(200, 17));
+                    DefaultBoundedRangeModel rangeModel = new DefaultBoundedRangeModel();
+                    rangeModel.setMaximum(100);
+                    rangeModel.setMinimum(0);
+                    rangeModel.setExtent(50);
+                    rangeModel.setValue(50);
 
-                sb.setModel(rangeModel);
-                frame.add(sb, BorderLayout.NORTH);
+                    sb.setModel(rangeModel);
+                    frame.add(sb, BorderLayout.NORTH);
 
-                frame.setSize(200, 100);
-                frame.setVisible(true);
-            }
-        });
-
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                thumbBounds[0] = new Rectangle(ui.getThumbBounds());
-
-                Point l = sb.getLocationOnScreen();
-
-                robot.mouseMove(l.x + (int) (0.75 * sb.getWidth()), l.y + sb.getHeight() / 2);
-                robot.mousePress(InputEvent.BUTTON1_MASK);
-                robot.mouseRelease(InputEvent.BUTTON1_MASK);
-            }
-        });
-
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                Rectangle newThumbBounds = ui.getThumbBounds();
-
-                if (!thumbBounds[0].equals(newThumbBounds)) {
-                    throw new RuntimeException("Test failed.\nOld bounds: " + thumbBounds[0] +
-                    "\nNew bounds: " + newThumbBounds);
+                    frame.setSize(200, 100);
+                    frame.setVisible(true);
                 }
-            }
-        });
+            });
+
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    thumbBounds[0] = new Rectangle(ui.getThumbBounds());
+
+                    Point l = sb.getLocationOnScreen();
+
+                    robot.mouseMove(l.x + (int) (0.75 * sb.getWidth()), l.y + sb.getHeight() / 2);
+                    robot.mousePress(InputEvent.BUTTON1_MASK);
+                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
+                }
+            });
+
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    Rectangle newThumbBounds = ui.getThumbBounds();
+
+                    if (!thumbBounds[0].equals(newThumbBounds)) {
+                        throw new RuntimeException("Test failed.\nOld bounds: " + thumbBounds[0] +
+                        "\nNew bounds: " + newThumbBounds);
+                    }
+                }
+            });
+        } finally {
+                if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     static class MyScrollBarUI extends BasicScrollBarUI {
diff --git a/test/jdk/javax/swing/JScrollBar/bug4202954/bug4202954.java b/test/jdk/javax/swing/JScrollBar/bug4202954/bug4202954.java
index 2cc0d21..d9dc7e2 100644
--- a/test/jdk/javax/swing/JScrollBar/bug4202954/bug4202954.java
+++ b/test/jdk/javax/swing/JScrollBar/bug4202954/bug4202954.java
@@ -40,50 +40,55 @@
 public class bug4202954 {
     static JScrollPane buttonScrollPane;
     static Robot robot;
+    static JFrame testFrame;
     public static void main(String[] args) throws Exception {
-        if (Platform.isOSX()) {
-            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
-        }
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
+        try {
+            if (Platform.isOSX()) {
+                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
             }
-        });
-        Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane);
-        JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);
-        JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);
 
-        if (rightScrollButton == null || bottomScrollButton == null) {
-            String errMessage = "Test can't be executed: ";
-            errMessage = errMessage + (rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : ""
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+            Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane);
+            JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);
+            JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);
+
+            if (rightScrollButton == null || bottomScrollButton == null) {
+                String errMessage = "Test can't be executed: ";
+                errMessage = errMessage + (rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : ""
                     + (bottomScrollButton == null ? "can't find bottom scroll button for vertical scroll bar" : ""));
-            throw new RuntimeException(errMessage);
-        }
+                throw new RuntimeException(errMessage);
+            }
 
-        robot = new Robot();
-        robot.setAutoDelay(50);
+            robot = new Robot();
+            robot.setAutoDelay(50);
 
-        // test right, left and middle mouse buttons for horizontal scroll bar
-        if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {
-            throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button.");
-        }
-        if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {
-            throw new RuntimeException("Test failed: right arrow button respond on right mouse button.");
-        }
-        if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {
-            throw new RuntimeException("Test failed: right arrow button respond on middle mouse button.");
-        }
+            // test right, left and middle mouse buttons for horizontal scroll bar
+            if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {
+                throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button.");
+            }
+            if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {
+                throw new RuntimeException("Test failed: right arrow button respond on right mouse button.");
+            }
+            if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {
+                throw new RuntimeException("Test failed: right arrow button respond on middle mouse button.");
+            }
 
-        // test right, left and middle mouse buttons for vertical scroll bar
-        if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {
-            throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button.");
-        }
-        if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {
-            throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button.");
-        }
-        if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {
-            throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button.");
+            // test right, left and middle mouse buttons for vertical scroll bar
+            if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {
+                throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button.");
+            }
+            if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {
+                throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button.");
+            }
+            if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {
+                throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button.");
+            }
+        } finally {
+                if (testFrame != null) SwingUtilities.invokeAndWait(() -> testFrame.dispose());
         }
     }
     public static void createAndShowGUI() {
@@ -97,7 +102,7 @@
         buttonScrollPane = new JScrollPane();
         buttonScrollPane.setViewportView(buttonPanel);
 
-        JFrame testFrame = new JFrame("bug4202954");
+        testFrame = new JFrame("bug4202954");
         testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         testFrame.setLayout(new BorderLayout());
         testFrame.add(BorderLayout.CENTER, buttonScrollPane);
diff --git a/test/jdk/javax/swing/JSpinner/4973721/bug4973721.java b/test/jdk/javax/swing/JSpinner/4973721/bug4973721.java
index 9c18583..e3e1fd6 100644
--- a/test/jdk/javax/swing/JSpinner/4973721/bug4973721.java
+++ b/test/jdk/javax/swing/JSpinner/4973721/bug4973721.java
@@ -44,6 +44,7 @@
     static volatile boolean bFocusGained = false;
     static JSpinner spinner;
     static final Object listener = new bug4973721();
+    static JFrame frame;
 
     public void focusLost(FocusEvent e) {}
 
@@ -62,58 +63,62 @@
     public static void main(String[] args) throws Exception {
         UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                final JFrame frame = new JFrame();
-                spinner = new JSpinner();
-                frame.getContentPane().add(spinner);
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame();
+                    spinner = new JSpinner();
+                    frame.getContentPane().add(spinner);
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-                frame.pack();
-                frame.setVisible(true);
-                spinner.addChangeListener((ChangeListener)listener);
-                spinner.addFocusListener((FocusListener)listener);
-                spinner.requestFocus();
+                    frame.pack();
+                    frame.setVisible(true);
+                    spinner.addChangeListener((ChangeListener)listener);
+                    spinner.addFocusListener((FocusListener)listener);
+                    spinner.requestFocus();
 
+                }
+            });
+
+            synchronized(listener) {
+                if (!bFocusGained) {
+                    System.out.println("waiting focusGained...");
+                    try { listener.wait(5000); } catch (InterruptedException e) {}
+                }
             }
-        });
 
-        synchronized(listener) {
-            if (!bFocusGained) {
-                System.out.println("waiting focusGained...");
-                try { listener.wait(5000); } catch (InterruptedException e) {}
+            boolean hasFocus = Util.invokeOnEDT(new java.util.concurrent.Callable<Boolean>() {
+                @Override
+                public Boolean call() throws Exception {
+                    return spinner.hasFocus();
+                }
+            });
+
+            if (!bFocusGained && !hasFocus) {
+                throw new RuntimeException("Couldn't request focus for spinner");
             }
-        }
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        boolean hasFocus = Util.invokeOnEDT(new java.util.concurrent.Callable<Boolean>() {
-            @Override
-            public Boolean call() throws Exception {
-                return spinner.hasFocus();
+            Util.hitKeys(robot, KeyEvent.VK_UP);
+            robot.waitForIdle();
+            Thread.sleep(1000);
+
+            if (!bStateChanged) {
+                throw new RuntimeException("Up arrow key button doesn't work for a spinner in Synth L&F");
             }
-        });
 
-        if (!bFocusGained && !hasFocus) {
-            throw new RuntimeException("Couldn't request focus for spinner");
-        }
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
+            bStateChanged = false;
 
-        Util.hitKeys(robot, KeyEvent.VK_UP);
-        robot.waitForIdle();
-        Thread.sleep(1000);
+            Util.hitKeys(robot, KeyEvent.VK_DOWN);
+            robot.waitForIdle();
+            Thread.sleep(1000);
 
-        if (!bStateChanged) {
-            throw new RuntimeException("Up arrow key button doesn't work for a spinner in Synth L&F");
-        }
-
-        bStateChanged = false;
-
-        Util.hitKeys(robot, KeyEvent.VK_DOWN);
-        robot.waitForIdle();
-        Thread.sleep(1000);
-
-        if (!bStateChanged) {
-            throw new RuntimeException("Down arrow key button doesn't work for a spinner in Synth L&F");
+            if (!bStateChanged) {
+                throw new RuntimeException("Down arrow key button doesn't work for a spinner in Synth L&F");
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JSplitPane/4885629/bug4885629.java b/test/jdk/javax/swing/JSplitPane/4885629/bug4885629.java
index 59db070..c7e9800 100644
--- a/test/jdk/javax/swing/JSplitPane/4885629/bug4885629.java
+++ b/test/jdk/javax/swing/JSplitPane/4885629/bug4885629.java
@@ -46,6 +46,7 @@
     private static final Color BGCOLOR = Color.blue;
 
     private static JSplitPane sp;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
         UIManager.setLookAndFeel(new BasicLookAndFeel() {
@@ -56,58 +57,62 @@
                 public String getName() { return "FooName"; }
         });
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                JFrame frame = new JFrame();
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    frame = new JFrame();
 
-                JComponent a = new JPanel();
-                a.setBackground(Color.white);
-                a.setMinimumSize(new Dimension(10, 10));
+                    JComponent a = new JPanel();
+                    a.setBackground(Color.white);
+                    a.setMinimumSize(new Dimension(10, 10));
 
-                JComponent b = new JPanel();
-                b.setBackground(Color.white);
-                b.setMinimumSize(new Dimension(10, 10));
+                    JComponent b = new JPanel();
+                    b.setBackground(Color.white);
+                    b.setMinimumSize(new Dimension(10, 10));
 
-                sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, a, b);
-                sp.setPreferredSize(new Dimension(20, 20));
-                sp.setBackground(BGCOLOR);
+                    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, a, b);
+                    sp.setPreferredSize(new Dimension(20, 20));
+                    sp.setBackground(BGCOLOR);
 
-                Border bo = new BasicBorders.SplitPaneBorder(lightHighlight,
-                        Color.red);
-                Border ibo = new EmptyBorder(0, 0, 0, 0);
-                sp.setBorder(bo);
-                sp.setMinimumSize(new Dimension(200, 200));
+                    Border bo = new BasicBorders.SplitPaneBorder(lightHighlight,
+                            Color.red);
+                    Border ibo = new EmptyBorder(0, 0, 0, 0);
+                    sp.setBorder(bo);
+                    sp.setMinimumSize(new Dimension(200, 200));
 
-                ((BasicSplitPaneUI) sp.getUI()).getDivider().setBorder(ibo);
+                    ((BasicSplitPaneUI) sp.getUI()).getDivider().setBorder(ibo);
 
-                frame.getContentPane().setLayout(new FlowLayout());
-                frame.getContentPane().setBackground(darkShadow);
-                frame.getContentPane().add(sp);
+                    frame.getContentPane().setLayout(new FlowLayout());
+                    frame.getContentPane().setBackground(darkShadow);
+                    frame.getContentPane().add(sp);
 
-                frame.setSize(200, 200);
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                frame.setVisible(true);
-            }
-        });
+                    frame.setSize(200, 200);
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    frame.setVisible(true);
+                }
+            });
 
-        final Robot robot = new Robot();
-        robot.waitForIdle();
-        robot.delay(1000);
+            final Robot robot = new Robot();
+            robot.waitForIdle();
+            robot.delay(1000);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                Rectangle rect = ((BasicSplitPaneUI) sp.getUI()).getDivider().getBounds();
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    Rectangle rect = ((BasicSplitPaneUI) sp.getUI()).getDivider().getBounds();
 
-                Point p = rect.getLocation();
+                    Point p = rect.getLocation();
 
-                SwingUtilities.convertPointToScreen(p, sp);
+                    SwingUtilities.convertPointToScreen(p, sp);
 
-                for (int i = 0; i < rect.width; i++) {
-                    if (!BGCOLOR.equals(robot.getPixelColor(p.x + i, p.y + rect.height - 1))) {
-                        throw new Error("The divider's area has incorrect color.");
+                    for (int i = 0; i < rect.width; i++) {
+                        if (!BGCOLOR.equals(robot.getPixelColor(p.x + i, p.y + rect.height - 1))) {
+                            throw new Error("The divider's area has incorrect color.");
+                        }
                     }
                 }
-            }
-        });
+            });
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 }
diff --git a/test/jdk/javax/swing/JTabbedPane/4361477/bug4361477.java b/test/jdk/javax/swing/JTabbedPane/4361477/bug4361477.java
index 20ca62e..6cf6231 100644
--- a/test/jdk/javax/swing/JTabbedPane/4361477/bug4361477.java
+++ b/test/jdk/javax/swing/JTabbedPane/4361477/bug4361477.java
@@ -39,44 +39,49 @@
     static JTabbedPane tabbedPane;
     volatile static boolean bStateChanged = false;
     volatile static Rectangle bounds;
+    static JFrame frame;
 
     public static void main(String args[]) throws Exception {
+        try {
 
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                createAndShowUI();
+                @Override
+                public void run() {
+                    createAndShowUI();
+                }
+            });
+
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    bounds = tabbedPane.getUI().getTabBounds(tabbedPane, 0);
+                }
+            });
+
+            Point location = bounds.getLocation();
+            SwingUtilities.convertPointToScreen(location, tabbedPane);
+            robot.mouseMove(location.x + 1, location.y + 1);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+
+            if (!bStateChanged) {
+                throw new RuntimeException("Tabbed pane state is not changed");
             }
-        });
-
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                bounds = tabbedPane.getUI().getTabBounds(tabbedPane, 0);
-            }
-        });
-
-        Point location = bounds.getLocation();
-        SwingUtilities.convertPointToScreen(location, tabbedPane);
-        robot.mouseMove(location.x + 1, location.y + 1);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
-
-        if (!bStateChanged) {
-            throw new RuntimeException("Tabbed pane state is not changed");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
     static void createAndShowUI() {
 
-        final JFrame frame = new JFrame();
+        frame = new JFrame();
         tabbedPane = new JTabbedPane();
         tabbedPane.add("Tab0", new JPanel());
         tabbedPane.add("Tab1", new JPanel());
diff --git a/test/jdk/javax/swing/JTabbedPane/4624207/bug4624207.java b/test/jdk/javax/swing/JTabbedPane/4624207/bug4624207.java
index 8808ae7..10de2ab 100644
--- a/test/jdk/javax/swing/JTabbedPane/4624207/bug4624207.java
+++ b/test/jdk/javax/swing/JTabbedPane/4624207/bug4624207.java
@@ -49,6 +49,7 @@
     private static JTextField txtField;
     private static JTabbedPane tab;
     private static Object listener;
+    private static JFrame frame;
 
     public void stateChanged(ChangeEvent e) {
         System.out.println("stateChanged called");
@@ -66,51 +67,55 @@
     }
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            public void run() {
-                createAndShowGUI();
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+
+            robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                public void run() {
+                    txtField.requestFocus();
+                }
+            });
+
+            robot.waitForIdle();
+
+            if (!focusGained) {
+                throw new RuntimeException("Couldn't gain focus for text field");
             }
-        });
 
-        robot.waitForIdle();
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    tab.addChangeListener((ChangeListener) listener);
+                    txtField.removeFocusListener((FocusListener) listener);
+                }
+            });
 
-            public void run() {
-                txtField.requestFocus();
+            robot.waitForIdle();
+
+            if (Platform.isOSX()) {
+                Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_B);
+            } else {
+                Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_B);
             }
-        });
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        if (!focusGained) {
-            throw new RuntimeException("Couldn't gain focus for text field");
-        }
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            public void run() {
-                tab.addChangeListener((ChangeListener) listener);
-                txtField.removeFocusListener((FocusListener) listener);
+            if (!stateChanged || tab.getSelectedIndex() != 1) {
+                throw new RuntimeException("JTabbedPane mnemonics don't work from outside the tabbed pane");
             }
-        });
-
-        robot.waitForIdle();
-
-        if (Platform.isOSX()) {
-            Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_B);
-        } else {
-            Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_B);
-        }
-
-        robot.waitForIdle();
-
-        if (!stateChanged || tab.getSelectedIndex() != 1) {
-            throw new RuntimeException("JTabbedPane mnemonics don't work from outside the tabbed pane");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() ->  frame.dispose());
         }
     }
 
@@ -121,7 +126,7 @@
         tab.setMnemonicAt(0, KeyEvent.VK_T);
         tab.setMnemonicAt(1, KeyEvent.VK_B);
 
-        JFrame frame = new JFrame();
+        frame = new JFrame();
         frame.getContentPane().add(tab, BorderLayout.CENTER);
         txtField = new JTextField();
         frame.getContentPane().add(txtField, BorderLayout.NORTH);
diff --git a/test/jdk/javax/swing/JTabbedPane/6495408/bug6495408.java b/test/jdk/javax/swing/JTabbedPane/6495408/bug6495408.java
index 25da3f8..f0ecf77 100644
--- a/test/jdk/javax/swing/JTabbedPane/6495408/bug6495408.java
+++ b/test/jdk/javax/swing/JTabbedPane/6495408/bug6495408.java
@@ -35,46 +35,51 @@
 public class bug6495408 {
 
     static JTabbedPane tabbedPane;
+    static JFrame frame;
 
     public static void main(String[] args) throws Exception {
-        final Robot robot = new Robot();
-        robot.setAutoDelay(50);
+        try {
+            final Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            public void run() {
-                final JFrame frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                tabbedPane = new JTabbedPane();
-                tabbedPane.setTabPlacement(JTabbedPane.LEFT);
-                tabbedPane.addTab("Hello", null);
-                frame.add(tabbedPane);
-                frame.setSize(400, 400);
-                frame.setLocationRelativeTo(null);
-                frame.setVisible(true);
-            }
-        });
-
-        robot.waitForIdle();
-
-        final Rectangle d = new Rectangle();
-        final Point p = new Point();
-
-        for (int i = 0; i < 7; i++) {
-            SwingUtilities.invokeLater(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
                 public void run() {
-                    int tab = tabbedPane.getTabCount() - 1;
-                    Rectangle bounds = tabbedPane.getBoundsAt(tab);
-                    if (bounds != null) {
-                        d.setBounds(bounds);
-                        p.setLocation(d.x + d.width / 2, d.y + d.height / 2);
-                        SwingUtilities.convertPointToScreen(p, tabbedPane);
-                        robot.mouseMove(p.x, p.y + d.height);
-                        tabbedPane.addTab("Hello", null);
-                    }
+                    frame = new JFrame();
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    tabbedPane = new JTabbedPane();
+                    tabbedPane.setTabPlacement(JTabbedPane.LEFT);
+                    tabbedPane.addTab("Hello", null);
+                    frame.add(tabbedPane);
+                    frame.setSize(400, 400);
+                    frame.setLocationRelativeTo(null);
+                    frame.setVisible(true);
                 }
             });
+
+            robot.waitForIdle();
+
+            final Rectangle d = new Rectangle();
+            final Point p = new Point();
+
+            for (int i = 0; i < 7; i++) {
+                SwingUtilities.invokeLater(new Runnable() {
+
+                    public void run() {
+                        int tab = tabbedPane.getTabCount() - 1;
+                        Rectangle bounds = tabbedPane.getBoundsAt(tab);
+                        if (bounds != null) {
+                            d.setBounds(bounds);
+                            p.setLocation(d.x + d.width / 2, d.y + d.height / 2);
+                            SwingUtilities.convertPointToScreen(p, tabbedPane);
+                            robot.mouseMove(p.x, p.y + d.height);
+                            tabbedPane.addTab("Hello", null);
+                        }
+                    }
+                });
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 }
diff --git a/test/jdk/javax/swing/JTabbedPane/7161568/bug7161568.java b/test/jdk/javax/swing/JTabbedPane/7161568/bug7161568.java
index 1904a3f..9561eea 100644
--- a/test/jdk/javax/swing/JTabbedPane/7161568/bug7161568.java
+++ b/test/jdk/javax/swing/JTabbedPane/7161568/bug7161568.java
@@ -36,42 +36,47 @@
 
     private static final int N = 50;
     private static JTabbedPane tabbedPane;
+    private static JFrame frame;
 
     public static void main(String[] args) throws Exception {
         UIManager.put("TabbedPane.selectionFollowsFocus", Boolean.FALSE);
 
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        SwingUtilities.invokeAndWait(new Runnable() {
+            SwingUtilities.invokeAndWait(new Runnable() {
 
-            @Override
-            public void run() {
-                createAndShowUI();
-            }
-        });
+                @Override
+                public void run() {
+                    createAndShowUI();
+                }
+            });
 
-        robot.waitForIdle();
-
-        SwingUtilities.invokeAndWait(new Runnable() {
-
-            @Override
-            public void run() {
-                tabbedPane.requestFocus();
-            }
-        });
-
-        robot.waitForIdle();
-
-        for (int i = 0; i < N; i++) {
-            robot.keyPress(KeyEvent.VK_LEFT);
-            robot.keyRelease(KeyEvent.VK_LEFT);
             robot.waitForIdle();
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    tabbedPane.requestFocus();
+                }
+            });
+
+            robot.waitForIdle();
+
+            for (int i = 0; i < N; i++) {
+                robot.keyPress(KeyEvent.VK_LEFT);
+                robot.keyRelease(KeyEvent.VK_LEFT);
+                robot.waitForIdle();
+            }
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
     static void createAndShowUI() {
-        JFrame frame = new JFrame("Test");
+        frame = new JFrame("Test");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(100, 100);
 
diff --git a/test/jdk/javax/swing/JTabbedPane/8134116/Bug8134116.java b/test/jdk/javax/swing/JTabbedPane/8134116/Bug8134116.java
index 1881666..97164c2 100644
--- a/test/jdk/javax/swing/JTabbedPane/8134116/Bug8134116.java
+++ b/test/jdk/javax/swing/JTabbedPane/8134116/Bug8134116.java
@@ -20,6 +20,7 @@
 public class Bug8134116 {
 
     private static volatile Exception exception = null;
+    private static JFrame frame;
 
     public static void main(String args[]) throws Exception {
 
@@ -29,139 +30,143 @@
             throw new RuntimeException(e);
         }
 
-        SwingUtilities.invokeAndWait(() -> {
-            JPanel panel0 = new JPanel();
-            JPanel panel2 = new JPanel();
-            BadPane badPane = new BadPane();
-            badPane.add("zero", panel0);
-            badPane.add("one", null);  // no component
-            badPane.add("", panel2);  // no title
-            badPane.add("", null); // no component, no title
-            // but give it that via a tabComponent
-            JPanel tabComponent = new JPanel();
-            JLabel tabComponentLabel = new JLabel("three");
-            tabComponent.add(tabComponentLabel);
-            badPane.setTabComponentAt(3, tabComponent);
-            JFrame frame = new JFrame();
-            frame.add(badPane);
-            frame.setSize(300, 300);
-            frame.setVisible(true);
+        try {
+            SwingUtilities.invokeAndWait(() -> {
+                JPanel panel0 = new JPanel();
+                JPanel panel2 = new JPanel();
+                BadPane badPane = new BadPane();
+                badPane.add("zero", panel0);
+                badPane.add("one", null);  // no component
+                badPane.add("", panel2);  // no title
+                badPane.add("", null); // no component, no title
+                // but give it that via a tabComponent
+                JPanel tabComponent = new JPanel();
+                JLabel tabComponentLabel = new JLabel("three");
+                tabComponent.add(tabComponentLabel);
+                badPane.setTabComponentAt(3, tabComponent);
+                frame = new JFrame();
+                frame.add(badPane);
+                frame.setSize(300, 300);
+                frame.setVisible(true);
 
-            try {
-                AccessibleContext ac = badPane.getAccessibleContext();
-                Accessible page0 = ac.getAccessibleChild(0);
-                if (page0 == null) {
-                    // Not something being tested, but checking anyway
-                    throw new RuntimeException("getAccessibleChild(0) is null");
-                }
-                Accessible page1 = ac.getAccessibleChild(1);
-                if (page1 == null) {
-                    // Not something being tested, but checking anyway
-                    throw new RuntimeException("getAccessibleChild(1) is null");
-                }
-                Accessible page2 = ac.getAccessibleChild(2);
-                Accessible page3 = ac.getAccessibleChild(3);
-                // page0 - page3 are JTabbedPane.Page, a private inner class
-                // and is an AccessibleContext
-                // and implements Accessible and AccessibleComponent
-                AccessibleContext pac0 = page0.getAccessibleContext();
-                AccessibleContext pac1 = page1.getAccessibleContext();
-                AccessibleContext pac2 = page2.getAccessibleContext();
-                AccessibleContext pac3 = page3.getAccessibleContext();
+                try {
+                    AccessibleContext ac = badPane.getAccessibleContext();
+                    Accessible page0 = ac.getAccessibleChild(0);
+                    if (page0 == null) {
+                        // Not something being tested, but checking anyway
+                        throw new RuntimeException("getAccessibleChild(0) is null");
+                    }
+                    Accessible page1 = ac.getAccessibleChild(1);
+                    if (page1 == null) {
+                        // Not something being tested, but checking anyway
+                        throw new RuntimeException("getAccessibleChild(1) is null");
+                    }
+                    Accessible page2 = ac.getAccessibleChild(2);
+                    Accessible page3 = ac.getAccessibleChild(3);
+                    // page0 - page3 are JTabbedPane.Page, a private inner class
+                    // and is an AccessibleContext
+                    // and implements Accessible and AccessibleComponent
+                    AccessibleContext pac0 = page0.getAccessibleContext();
+                    AccessibleContext pac1 = page1.getAccessibleContext();
+                    AccessibleContext pac2 = page2.getAccessibleContext();
+                    AccessibleContext pac3 = page3.getAccessibleContext();
 
-                // test Page.getBounds
-                // ensure no IndexOutOfBoundsException
-                Rectangle r0 = pac0.getAccessibleComponent().getBounds();
-                // make sure second Bounds is different than first
-                Rectangle r1  = pac1.getAccessibleComponent().getBounds();
-                if (r1.equals(r0)) {
-                    String msg = "Second tab should not have same bounds as first tab";
-                    throw new RuntimeException(msg);
-                }
+                    // test Page.getBounds
+                    // ensure no IndexOutOfBoundsException
+                    Rectangle r0 = pac0.getAccessibleComponent().getBounds();
+                    // make sure second Bounds is different than first
+                    Rectangle r1  = pac1.getAccessibleComponent().getBounds();
+                    if (r1.equals(r0)) {
+                        String msg = "Second tab should not have same bounds as first tab";
+                        throw new RuntimeException(msg);
+                    }
 
-                // test Page.getAccessibleStateSet
-                // At this point page 0 is selected
-                AccessibleStateSet accSS0 = pac0.getAccessibleStateSet();
-                if (!accSS0.contains(AccessibleState.SELECTED)) {
-                    String msg = "Empty title -> AccessibleState.SELECTED not set";
-                    throw new RuntimeException(msg);
-                }
-                // select second tab
-                badPane.setSelectedIndex(1);
-                AccessibleStateSet accSS1 = pac1.getAccessibleStateSet();
-                if (!accSS1.contains(AccessibleState.SELECTED)) {
-                    String msg = "Second tab selected but AccessibleState.SELECTED not set";
-                    throw new RuntimeException(msg);
-                }
-                // select third tab
-                badPane.setSelectedIndex(2);
-                AccessibleStateSet accSS2 = pac2.getAccessibleStateSet();
-                if (!accSS1.contains(AccessibleState.SELECTED)) {
-                    String msg = "Third tab selected but AccessibleState.SELECTED not set";
-                    throw new RuntimeException(msg);
-                }
-                // select fourth tab
-                badPane.setSelectedIndex(3);
-                AccessibleStateSet accSS3 = pac3.getAccessibleStateSet();
-                if (!accSS1.contains(AccessibleState.SELECTED)) {
-                    String msg = "Fourth tab selected but AccessibleState.SELECTED not set";
-                    throw new RuntimeException(msg);
-                }
+                    // test Page.getAccessibleStateSet
+                    // At this point page 0 is selected
+                    AccessibleStateSet accSS0 = pac0.getAccessibleStateSet();
+                    if (!accSS0.contains(AccessibleState.SELECTED)) {
+                        String msg = "Empty title -> AccessibleState.SELECTED not set";
+                        throw new RuntimeException(msg);
+                    }
+                    // select second tab
+                    badPane.setSelectedIndex(1);
+                    AccessibleStateSet accSS1 = pac1.getAccessibleStateSet();
+                    if (!accSS1.contains(AccessibleState.SELECTED)) {
+                        String msg = "Second tab selected but AccessibleState.SELECTED not set";
+                        throw new RuntimeException(msg);
+                    }
+                    // select third tab
+                    badPane.setSelectedIndex(2);
+                    AccessibleStateSet accSS2 = pac2.getAccessibleStateSet();
+                    if (!accSS1.contains(AccessibleState.SELECTED)) {
+                        String msg = "Third tab selected but AccessibleState.SELECTED not set";
+                        throw new RuntimeException(msg);
+                    }
+                    // select fourth tab
+                    badPane.setSelectedIndex(3);
+                    AccessibleStateSet accSS3 = pac3.getAccessibleStateSet();
+                    if (!accSS1.contains(AccessibleState.SELECTED)) {
+                        String msg = "Fourth tab selected but AccessibleState.SELECTED not set";
+                        throw new RuntimeException(msg);
+                    }
 
-                // test Page.getAccessibleIndexInParent
-                if (pac0.getAccessibleIndexInParent() == -1) {
-                    String msg = "Empty title -> negative AccessibleIndexInParent";
-                    throw new RuntimeException(msg);
-                }
-                if (pac0.getAccessibleIndexInParent() != 0) {
-                    String msg = "first tab is not at index 0 in parent";
-                    throw new RuntimeException(msg);
-                }
-                if (pac1.getAccessibleIndexInParent() != 1) {
-                    String msg = "second tab (null component) is not at index 1 in parent";
-                    throw new RuntimeException(msg);
-                }
-                if (pac2.getAccessibleIndexInParent() != 2) {
-                    String msg = "third tab (empty title) string is not at index 2 in parent";
-                    throw new RuntimeException(msg);
-                }
-                if (pac3.getAccessibleIndexInParent() != 3) {
-                    String msg = "fourth tab (empty title, null component, has tabComponent) string is not at index 3 in parent";
-                    throw new RuntimeException(msg);
-                }
+                    // test Page.getAccessibleIndexInParent
+                    if (pac0.getAccessibleIndexInParent() == -1) {
+                        String msg = "Empty title -> negative AccessibleIndexInParent";
+                        throw new RuntimeException(msg);
+                    }
+                    if (pac0.getAccessibleIndexInParent() != 0) {
+                        String msg = "first tab is not at index 0 in parent";
+                        throw new RuntimeException(msg);
+                    }
+                    if (pac1.getAccessibleIndexInParent() != 1) {
+                        String msg = "second tab (null component) is not at index 1 in parent";
+                        throw new RuntimeException(msg);
+                    }
+                    if (pac2.getAccessibleIndexInParent() != 2) {
+                        String msg = "third tab (empty title) string is not at index 2 in parent";
+                        throw new RuntimeException(msg);
+                    }
+                    if (pac3.getAccessibleIndexInParent() != 3) {
+                        String msg = "fourth tab (empty title, null component, has tabComponent) string is not at index 3 in parent";
+                        throw new RuntimeException(msg);
+                    }
 
-                // test Page.getAccessibleName
-                String accName = pac0.getAccessibleName();
-                if (!accName.equals("zero")) {
-                    String msg = "Empty title -> empty AccessibleName";
-                    throw new RuntimeException(msg);
-                }
-                // test Page.getAccessibleName when component is null
-                accName = pac1.getAccessibleName();
-                if (!accName.equals("one")) {
-                    String msg = "AccessibleName of null panel not 'one'";
-                    throw new RuntimeException(msg);
-                }
+                    // test Page.getAccessibleName
+                    String accName = pac0.getAccessibleName();
+                    if (!accName.equals("zero")) {
+                        String msg = "Empty title -> empty AccessibleName";
+                        throw new RuntimeException(msg);
+                    }
+                    // test Page.getAccessibleName when component is null
+                    accName = pac1.getAccessibleName();
+                    if (!accName.equals("one")) {
+                        String msg = "AccessibleName of null panel not 'one'";
+                        throw new RuntimeException(msg);
+                    }
 
-                // test Page.setDisplayedMnemonicIndex
-                //  Empty title -> IllegalArgumnetException
-                badPane.setDisplayedMnemonicIndexAt(0, 1);
+                    // test Page.setDisplayedMnemonicIndex
+                    //  Empty title -> IllegalArgumnetException
+                    badPane.setDisplayedMnemonicIndexAt(0, 1);
 
-                // test Page.updateDisplayedMnemonicIndex
-                badPane.setMnemonicAt(0, KeyEvent.VK_Z);
-                if (badPane.getDisplayedMnemonicIndexAt(0) == -1) {
-                    String msg="Empty title -> getDisplayedMnemonicIndexAt failure";
-                    throw new RuntimeException(msg);
+                    // test Page.updateDisplayedMnemonicIndex
+                    badPane.setMnemonicAt(0, KeyEvent.VK_Z);
+                    if (badPane.getDisplayedMnemonicIndexAt(0) == -1) {
+                        String msg="Empty title -> getDisplayedMnemonicIndexAt failure";
+                        throw new RuntimeException(msg);
+                    }
+                } catch (Exception e) {
+                    exception = e;
                 }
-            } catch (Exception e) {
-                exception = e;
+            });
+            if (exception != null) {
+                System.out.println("Test failed: " + exception.getMessage());
+                throw exception;
+            } else {
+                System.out.println("Test passed.");
             }
-        });
-        if (exception != null) {
-            System.out.println("Test failed: " + exception.getMessage());
-            throw exception;
-        } else {
-            System.out.println("Test passed.");
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
     }
 
diff --git a/test/jdk/javax/swing/JTable/4220171/bug4220171.java b/test/jdk/javax/swing/JTable/4220171/bug4220171.java
index 0dc904b..14cc80f 100644
--- a/test/jdk/javax/swing/JTable/4220171/bug4220171.java
+++ b/test/jdk/javax/swing/JTable/4220171/bug4220171.java
@@ -43,40 +43,45 @@
 public class bug4220171 {
 
     private static JTable table;
+    private static JFrame frame;
 
     public static void main(String args[]) throws Exception {
+        try {
 
-        Robot robot = new Robot();
-        robot.setAutoDelay(50);
+            Robot robot = new Robot();
+            robot.setAutoDelay(50);
 
-        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+            javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
 
-            public void run() {
-                createAndShowGUI();
-            }
-        });
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
 
-        robot.waitForIdle();
+            robot.waitForIdle();
 
-        clickMouse(robot, 0, 0);
-        Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_ENTER);
-        robot.waitForIdle();
-        checkCell(0, 0);
+            clickMouse(robot, 0, 0);
+            Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_ENTER);
+            robot.waitForIdle();
+            checkCell(0, 0);
 
-        clickMouse(robot, 0, 1);
-        Util.hitKeys(robot, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_ENTER);
-        robot.waitForIdle();
-        checkCell(0, 1);
+            clickMouse(robot, 0, 1);
+            Util.hitKeys(robot, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_ENTER);
+            robot.waitForIdle();
+            checkCell(0, 1);
 
-        clickMouse(robot, 1, 0);
-        Util.hitKeys(robot, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_ENTER);
-        robot.waitForIdle();
-        checkCell(1, 0);
+            clickMouse(robot, 1, 0);
+            Util.hitKeys(robot, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_ENTER);
+            robot.waitForIdle();
+            checkCell(1, 0);
 
-        clickMouse(robot, 1, 1);
-        Util.hitKeys(robot, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_ENTER);
-        robot.waitForIdle();
-        checkCell(1, 1);
+            clickMouse(robot, 1, 1);
+            Util.hitKeys(robot, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_ENTER);
+            robot.waitForIdle();
+            checkCell(1, 1);
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+        }
     }
 
     static void checkCell(final int row, final int column) throws Exception {
@@ -116,7 +121,7 @@
     }
 
     private static void createAndShowGUI() {
-        JFrame frame = new JFrame("Test");
+        frame = new JFrame("Test");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(200, 200);
 
diff --git a/test/jdk/javax/swing/JTable/6777378/bug6777378.java b/test/jdk/javax/swing/JTable/6777378/bug6777378.java
index b353249..b840ec3 100644
--- a/test/jdk/javax/swing/JTable/6777378/bug6777378.java
+++ b/test/jdk/javax/swing/JTable/6777378/bug6777378.java
@@ -43,45 +43,49 @@
     private static JTableHeader header;
 
     public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(20);
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                try {
-                    UIManager.setLookAndFeel(new MetalLookAndFeel());
-                } catch (Exception e) {
-                    e.printStackTrace();
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(20);
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    try {
+                        UIManager.setLookAndFeel(new MetalLookAndFeel());
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                    JTable table = new JTable(new AbstractTableModel() {
+                        public int getRowCount() {
+                            return 10;
+                        }
+
+                        public int getColumnCount() {
+                            return 10;
+                        }
+
+                        public Object getValueAt(int rowIndex, int columnIndex) {
+                            return "" + rowIndex + " " + columnIndex;
+                        }
+                    });
+
+                    header = new JTableHeader(table.getColumnModel());
+                    header.setToolTipText("hello");
+
+                    frame = new JFrame();
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    frame.add(header);
+
+                    frame.setSize(300, 300);
+                    frame.setVisible(true);
                 }
-                JTable table = new JTable(new AbstractTableModel() {
-                    public int getRowCount() {
-                        return 10;
-                    }
-
-                    public int getColumnCount() {
-                        return 10;
-                    }
-
-                    public Object getValueAt(int rowIndex, int columnIndex) {
-                        return "" + rowIndex + " " + columnIndex;
-                    }
-                });
-
-                header = new JTableHeader(table.getColumnModel());
-                header.setToolTipText("hello");
-
-                frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                frame.add(header);
-
-                frame.setSize(300, 300);
-                frame.setVisible(true);
-            }
-        });
-        robot.waitForIdle();
-        Point point = header.getLocationOnScreen();
-        robot.mouseMove(point.x + 20, point.y + 50);
-        robot.mouseMove(point.x + 30, point.y + 50);
-        robot.mousePress(InputEvent.BUTTON1_MASK);
-        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+            });
+            robot.waitForIdle();
+            Point point = header.getLocationOnScreen();
+            robot.mouseMove(point.x + 20, point.y + 50);
+            robot.mouseMove(point.x + 30, point.y + 50);
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        } finally {
+            if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
+         }
     }
 }