Search code examples
javaswingurljpaneljapplet

Open an url in a new tab from a Jpanel inside a JApplet


So my Applet work like that :

The main .class is extended from JApplet, therefore it can be use as an applet.

This main class load other .class file to display new window, that are extented from JPanel.

This current set up work fine as an applet, however, in one of my JPanel class I have a button that open an URL. I use the Desktop API and it works fine on the browser, the problem is : It opens the URL in the same tab as the applet.

I would like the URL to open in a new tab from my JPanel. I know I can use something like :

  AppletContext a = getAppletContext();
  URL url = new URL(link);
  a.showDocument(url,"_blank");

but the method getAppletContext() only work from a class that has extented JApplet, not a JPanel.

I have tried to change my Jpanel to a JApplet but that seems to create some kind of mess.

Any idea how I could achieve that ?

Thank you !


Solution

  • I haven't tried it, but two possibilities come to mind:

    • Pass the AppletContext to your JPanel as a constructor parameter.

    • Set the target attribute in the URL, as shown here.