7027700: /jfc/SwingApplet demo needs to be improved
authormrkam
Thu, 31 Mar 2011 10:15:08 -0700
changeset 8983 72a73724c353
parent 8981 3bf2f0d35e8a
child 8984 fbedde2feaa7
7027700: /jfc/SwingApplet demo needs to be improved Reviewed-by: alexp
jdk/src/share/demo/jfc/SwingApplet/SwingApplet.java
--- a/jdk/src/share/demo/jfc/SwingApplet/SwingApplet.java	Wed Mar 30 15:52:32 2011 -0700
+++ b/jdk/src/share/demo/jfc/SwingApplet/SwingApplet.java	Thu Mar 31 10:15:08 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,42 +29,59 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
-import java.awt.*;
-import java.awt.event.*;
-import java.net.*;
-import java.applet.*;
-import javax.swing.*;
+import java.awt.FlowLayout;
+import java.lang.reflect.InvocationTargetException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.JApplet;
+import javax.swing.JButton;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+
 
 /**
  * A very simple applet.
  */
+@SuppressWarnings("serial")
 public class SwingApplet extends JApplet {
 
     JButton button;
 
-    public void init() {
-
-        // Force SwingApplet to come up in the System L&F
-        String laf = UIManager.getSystemLookAndFeelClassName();
+    private void initUI() {
+        // Trying to set Nimbus look and feel
         try {
-            UIManager.setLookAndFeel(laf);
-            // If you want the Cross Platform L&F instead, comment out the above line and
-            // uncomment the following:
-            // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
-        } catch (UnsupportedLookAndFeelException exc) {
-            System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
-        } catch (Exception exc) {
-            System.err.println("Error loading " + laf + ": " + exc);
+            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
+        } catch (Exception ex) {
+            Logger.getLogger(SwingApplet.class.getName()).
+                    log(Level.SEVERE, "Failed to apply Nimbus look and feel", ex);
         }
-
         getContentPane().setLayout(new FlowLayout());
         button = new JButton("Hello, I'm a Swing Button!");
         getContentPane().add(button);
+        getContentPane().doLayout();
     }
 
+    @Override
+    public void init() {
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    initUI();
+                }
+            });
+        } catch (InterruptedException ex) {
+            Logger.getLogger(SwingApplet.class.getName()).
+                    log(Level.SEVERE, null, ex);
+        } catch (InvocationTargetException ex) {
+            Logger.getLogger(SwingApplet.class.getName()).
+                    log(Level.SEVERE, null, ex);
+        }
+    }
+
+    @Override
     public void stop() {
         if (button != null) {
             getContentPane().remove(button);