Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if my Java component is in an Applet?

Tags:

java

applet

I have a Component which I am using both in a standalone Java application as well as in a Java applet. How can I figure out from within the Component whether my component is in an applet?

Also, once I figure out that I'm running in an Applet, how can I get access to the Applet?

like image 782
Epaga Avatar asked Jan 16 '09 09:01

Epaga


People also ask

How do I know if an applet is working?

7.1. If your application will not run, perform the following checks: Verify that the Java Plugin is working. Go to the http://java.com/en/download/installed.jsp. Click on the Verify Java version button.

Does JDK 8 have applet?

As announced in 2015, Applets were supported in Java SE 8 until March, 2019. Although support is no longer available for Applets, they remain available for Windows and continue to receive updates in Java SE 8.

Where are Java applets executed?

Applets are executed inside the browser via the so-called "Java Plug-in", which is a JRE capable of running Java applets in a secure manner (just like most web browsers have plug-ins for running flash, JavaScript, VBScript and other programs).

What is the difference between application and applet in Java?

Applications are just like a Java program that can be executed independently without using the web browser. Applets are small Java programs that are designed to be included with the HTML web document. They require a Java-enabled web browser for execution.


2 Answers

You can do it without recursion by SwingUtilities.getAncestorOfClass

like image 153
Dennis C Avatar answered Oct 18 '22 04:10

Dennis C


I think you should be able to do it by repeatedly calling Component.getParent() until you get to the top of the container tree, and then checking whether that container is an instanceof Applet.

The code below is completely untested:

boolean isInAnApplet(Component c)
{
    Component p = c.getParent();
    if (p != null) {
         return isInAnApplet(p);
    } else {
         return (c instanceof Applet);
    }
}
like image 2
Alnitak Avatar answered Oct 18 '22 05:10

Alnitak