peterk
peterk

Reputation: 5444

what is the best way to test whether java code is running in an applet or not?

I want somewhere down in the java code:

if(inAnApplet()) {
    initForApplet();
} else {
    initForApp();
}

Upvotes: 0

Views: 204

Answers (3)

Andy
Andy

Reputation: 3682

Like jli said, maybe you could create initialise a public (static) boolean isApplet = true somewhere accessible in the class in which you would like to do the check and set that to false in your main method, as public static void main(String[] args) is not called in an Applet.

This way your check would simply start if(isApplet)!

HTH

Upvotes: 1

Gabriel Belingueres
Gabriel Belingueres

Reputation: 2975

public class MyApplet extends JApplet {

    boolean isapplet = true;


    public MyApplet() {
        this(true);
    }

    public MyApplet(boolean isapplet) {
        this.isapplet = isapplet;
    }

    public static final void main(String[] argv) {
        // is an app
        new MyApplet(false);
    }
}

Upvotes: 5

Tudor
Tudor

Reputation: 62439

Assuming that somewhere you have a view which extends JApplet and it is accessible from the code where you have the if, you can write:

if(mainView instanceof JApplet) {
     initForApplet();
} else {
     initForApp();
}

Upvotes: 2

Related Questions