0

I'm new to programming with Applets and wanted to make an Applet to put on a website. So here we go.

The Goal of this project was that if you click the button, it will open a JFrame on top of the browser. but while testing, it gives me a java.lang.ExceptionInInitializerError. This is the source code:

public class LaunchMenu extends Applet {

    public static LoginScreen login;
    public static Game game;
    public JButton button;
    public void init() {
        try {button= new JButton("Press this button to start");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login = new LoginScreen();

            }
        });
        } catch (Exception e) {
            e.getMessage();
            e.getCause();
        }
    }

    public void start() {
        login = new LoginScreen();
    }

    public void stop() {
        login.dispose();
        game.stop();
    }

    public static void main(String[] args) {

    }

}

note: It works in Eclipse with it's Applet window, but not on the website.

edit:

Exception in thread "AWT-EventQueue-2" java.lang.IllegalStateException: Applet's parent container not set up
    at sun.plugin2.applet.Plugin2Manager.start(Unknown Source)
    at sun.plugin2.main.client.PluginMain$StartAppletRunner.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
CacheEntry[http://localhost/AppletTest/Applet.jar]: updateAvailable=true,lastModified=Mon Apr 13 12:24:52 CEST 2015,length=5051938
4
  • A stack trace would be useful? Commented Apr 13, 2015 at 11:16
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to Why CS teachers should stop teaching Java applets. 2) Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing. Commented Apr 13, 2015 at 15:27
  • @AndrewThompson to answer your first question; the reason I am using applet is because I'm working on a project with someone. He's making the website, and I am making the Java related thing. And inorder to put it on the web, I am using applet. (The only thing I know for putting it on the web) :) And for your second question: Don't get me wrong, I use swing combined with AWT. Swing for all the visual parts, and awt for the ActionListeners & ItemsListeners ;) as it is "impossible not to." Commented Apr 13, 2015 at 18:49
  • "(The only thing I know for putting it on the web)" Launch a JFrame from a link using Java Web Start (it's much simpler). ".. I use swing combined with AWT. Swing for all the visual parts .." In that case, public class LaunchMenu extends Applet { should be public class LaunchMenu extends JApplet { (note the J in there). Commented Apr 13, 2015 at 19:02

1 Answer 1

1

Ok I fixed my problem. Here's the solution that worked for me.

SourceCode stays the same (except for a minor change)* :

public class LaunchMenu extends Applet {

    public static LoginScreen login;
    public static Game game;
    public JButton button;
    public void init() {
        try {button= new JButton("Start the game");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login = new LoginScreen();
                login.setVisible(true); *

            }
        });
        } catch (Exception e) {
            e.getMessage();
            e.getCause();
        }
    }

    public void start() {
        login = new LoginScreen();
    }

    public void stop() {
        login.dispose();
        game.stop();
    }

    public static void main(String[] args) {

    }
}

But I added a java.policy file in the same folder as the Applet. Within this file I wrote the following code:

grant { 
      permission java.security.AllPermission; 
}; 

After this was setup, I ran in another problem named java.lang.RuntimePermission: "exitVM.0"

The solution to this problem was simple. In the class with my JFrame, in my case LoginScreen, there was a line code setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. Delete this or comment it, this worked for me and now my Applet is visible and the Login frame opens. which was all I needed.

Hope this helps alot of people with the same problem.

Sign up to request clarification or add additional context in comments.

2 Comments

"But I added a java.policy file.." The applet will need to be digitally signed before it can be launched on the web (or by using Java Web Start). Probably best to figure that out now and add it to the build for the project.
"..there was a line code setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. Delete this or comment it.." It would be better to change it to setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);. That way, when the applet VM goes to end, it can shut down properly. (The default is DO_NOTIHNG_ON_CLOSE which hides the frame but leaves it in memory, ticking over..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.