Results 1 to 2 of 2
  1. #1
    jrohde's Avatar
    jrohde is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default Converting simple Java Application to Applet

    Hello,

    I am currently in Chapter 12 of Ivor Horton's book, "Beginning Java 2". When trying to convert the simple application shown below (which works fine when built as a standalone app in NetBeans) to a Java Applet, I get errors and neither Firefox nor IE will display the applet. These classes are verbatim from the text.

    The applet class, in "Sketcher.java":

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Sketcher extends JApplet {
    
        private static SketchFrame window;
        private static Sketcher theApp;
    
        public static void main(String[] args) {
            theApp = new Sketcher();
            theApp.init();
        }
    
        public void init() {
            window = new SketchFrame("Sketcher");
            Toolkit theKit = window.getToolkit();
            Dimension wndSize = theKit.getScreenSize();
            window.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2);
            window.setVisible(true);
        }
         
    }
    And in "SketchFrame.java":

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class SketchFrame extends JFrame {
    
        private JMenuBar menuBar = new JMenuBar();
        private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;
        private JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem;
        private JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;
    
        public SketchFrame(String title) {
            
            setTitle(title);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            setJMenuBar(menuBar);
    
            JMenu fileMenu = new JMenu("File");
            JMenu elementMenu = new JMenu("Elements");
            fileMenu.setMnemonic('F');
            elementMenu.setMnemonic('E');
    
            newItem = fileMenu.add("New");
            openItem = fileMenu.add("Open");
            closeItem = fileMenu.add("Close");
            fileMenu.addSeparator();
            saveItem = fileMenu.add("Save");
            saveAsItem = fileMenu.add("Save As");
            printItem = fileMenu.add("Print");
    
            elementMenu.add(lineItem = new JRadioButtonMenuItem("Line",true));
            elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle",false));
            elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle",false));
            elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve",false));
            
            curveItem.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK));
            lineItem.setAccelerator(KeyStroke.getKeyStroke('L',Event.CTRL_MASK));
            rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E',Event.CTRL_MASK));
            circleItem.setAccelerator(KeyStroke.getKeyStroke('I',Event.CTRL_MASK));
            
            ButtonGroup types = new ButtonGroup();
            types.add(lineItem);
            types.add(rectangleItem);
            types.add(circleItem);
            types.add(curveItem);
    
            elementMenu.addSeparator();
    
            JMenu colorMenu = new JMenu("Color");
            
            colorMenu.add(redItem = new JCheckBoxMenuItem("Red",false));
            colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow",false));
            colorMenu.add(greenItem = new JCheckBoxMenuItem("Green",false));
            colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue",true));
    
            redItem.setAccelerator(KeyStroke.getKeyStroke('R',Event.CTRL_MASK));
            yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y',Event.CTRL_MASK));
            greenItem.setAccelerator(KeyStroke.getKeyStroke('G',Event.CTRL_MASK));
            blueItem.setAccelerator(KeyStroke.getKeyStroke('B',Event.CTRL_MASK));
    
            elementMenu.add(colorMenu);
    
            menuBar.add(fileMenu);
            menuBar.add(elementMenu);
    
        }
    
        
    
    }
    Why does this fail when run as an applet as opposed to standalone? The errors, as reported by Firefox, are below. I see the words "security" and "access" in frequently in there, and being relatively new to Java, I'm not even prepared to attempt to understand that stuff yet.

    Thanks in advance for any and all help.

    java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    at java.security.AccessController.checkPermission(Unk nown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkExit(Unknown Source)
    at javax.swing.JFrame.setDefaultCloseOperation(Unknow n Source)
    at SketchFrame.<init>(SketchFrame.java:14)
    at Sketcher.init(Sketcher.java:15)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
    java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    at java.security.AccessController.checkPermission(Unk nown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkExit(Unknown Source)
    at javax.swing.JFrame.setDefaultCloseOperation(Unknow n Source)
    at SketchFrame.<init>(SketchFrame.java:14)
    at Sketcher.init(Sketcher.java:15)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I think this line
    Java Code:
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    is causing the AccessControlException. Try commenting it out.

    This is a little better way to set up the applet:
    Java Code:
    //  <applet code="Sketcher" width="300" height="300"></applet>
    //  use: prompt>appletviewer Sketcher.java
    import java.awt.*;
    import javax.swing.*;
    
    public class Sketcher extends JApplet {
    
        public static void main(String[] args) {
            JApplet applet = new Sketcher();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(applet);
            f.setSize(300,200);
            f.setLocation(100,100);
            applet.init();
            f.setVisible(true);
        }
    
        public void init() {
            JMenuBar menuBar = new SketchFrame("").getJMenuBar();
            setJMenuBar(menuBar);
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2009, 07:36 AM
  2. Replies: 1
    Last Post: 03-27-2009, 06:47 PM
  3. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 08:07 AM
  4. Converting netbeans app to applet
    By paulious in forum New To Java
    Replies: 3
    Last Post: 10-23-2008, 12:25 PM
  5. I need simple java applet
    By silvia in forum Java Applets
    Replies: 4
    Last Post: 01-27-2008, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •