Java - Opening a url in new window from an applet

This is the example of opening a url from an applet. This program shows that how a url is opened in a new document or browser.

Java - Opening a url in new window from an applet

     

This is the example of opening a url from an applet. This program shows that how a url is opened in a new document or browser. When you click the button "google", new browser window will open displaying the specified url, in our case url is http://www.google.com. This is accomplished by specifying the target as " _blank" while calling getAppletContext.showDocument(url, target) function.

 

 

Here is the code of the program : 

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;

public class testURL_NewWindow extends Applet implements ActionListener{

  public void init(){
  String link_Text = "google";
  Button b = new Button(link_Text);
  b.addActionListener(this);
  add(b);
  }

  public void actionPerformed(ActionEvent ae){
  //get the button label
  Button source = (Button)ae.getSource();
  String link = "http://www."+source.getLabel()+".com";
  try
  {
  AppletContext a = getAppletContext();
  URL url = new URL(link);
  a.showDocument(url,"_blank");
//  a.showDocument(url,"_self");
//  _self to open page in same window  
  }
  catch (MalformedURLException e){
  System.out.println(e.getMessage());
  }
  }
}

Try online this example.

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Java - Opening a url in new window from an applet

Posted on: December 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java - Opening a url in new window from an applet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads