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?
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.
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.
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).
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.
You can do it without recursion by SwingUtilities.getAncestorOfClass
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With