Open links from within the applet

suggest change

You can use the method getAppletContext() to get an AppletContext object that allows you to request the browser to open a link. For this you use the method showDocument(). Its second parameter tells the browser to use a new window _blank or the one that shows the applet _self.

public class MyLinkApplet extends JApplet{
    @Override
    public void init(){
        JButton button = new JButton("ClickMe!");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                AppletContext a = getAppletContext();                 
                try {
                    URL url = new URL("http://stackoverflow.com/");
                    a.showDocument(url,"_blank");
                } catch (Exception e) { /* omitted for brevity */ }   
            }
        });
        add(button);
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents