-1

We have an applet code below. where we have specified a button and when the button is clicked it should open a new website. Yahoo website in this case. The code for the applet is below

public class GotoLinkApplet extends Applet implements ActionListener{
public void init()
{
    String link="yahoo";
    Button b=new Button(link);
    b.addActionListener(this);
    add(b);

}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    System.out.println("1");
Button src= (Button)e.getSource();
String link="http://www."+src.getLabel()+".com";
try{
    AppletContext a=getAppletContext();
    URL url =new URL(link);
    //a.showDocument(url, "_self");
        a.showDocument(url, "_blank");
        System.out.println("a");
        }
catch(MalformedURLException ae)
{
    System.out.println(ae.getMessage());
}
}

}

We executed the above code in eclipse,but when we click on the button, the yahoo link is not coming up. Request you to help. But the code that specifies showdocument runs fine.

Any help on the above is much appreciated.

vr3w3c9
  • 1,118
  • 8
  • 32
  • 57
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. .. – Andrew Thompson Sep 03 '16 at 09:50
  • .. 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 4) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 5) AFAIR, the applet viewer does not implement `showDocument(..)` – Andrew Thompson Sep 03 '16 at 09:51
  • Possible duplicate of [How to open the default webbrowser using java](http://stackoverflow.com/questions/5226212/how-to-open-the-default-webbrowser-using-java) – progyammer Sep 15 '16 at 11:48

1 Answers1

0

This should be possible using Desktop.browse(uri);

You'll of course want to make sure the Desktop is supported first.

Jacob
  • 91
  • 1
  • 9