MyApplet.java

Download MyApplet.java

 1: // Signed MyApplet Demo - A simple Applet, signed.
 2: // Once signed, the user can allow extra permissions for any applet
 3: // (or WebStart) program.
 4: //
 5: // Written 8/2013 by Wayne Pollock, Tampa Florida USA
 6: // Updated 2/2015 by WP: Fixed swing GUI creation to use EDT.
 7: //                       Updated to use Java 8 method reference.
 8: // From original code by D. Liang (supplement to 9th ed of his book).
 9: 
10: package mypackage;
11: 
12: import java.io.*;
13: import javax.swing.*;
14: 
15: public class MyApplet extends JApplet {
16:    public void init () {
17:       // Create this JApplet's GUI on the event-dispatching thread:
18:       try {
19:          SwingUtilities.invokeAndWait( this::createGUI );
20:       } catch ( Exception e ) {
21:          System.err.println("createGUI didn't successfully complete");
22:       }
23:    }
24: 
25:    private void createGUI() {
26:       JLabel label = new JLabel();
27:       add( label );
28: 
29:       // The following code causes a security exception in an applet,
30:       // even if signed, if it runs in the "sandbox".  However, the
31:       // user can grant a signed applet extra (all) permissions; in fact,
32:       // that is the default for signed applets since Java 7U10.
33: 
34:       try {
35:          File file = new File( "." );
36:          label.setText( "The current directory is "
37:             + file.getAbsolutePath() );
38:       } catch ( Exception e ) {
39:          e.printStackTrace();
40:          label.setText(
41:             "Security exception while attempting access to current directory" );
42:       }
43:    }
44: }