Open a link in a new window using Applet

Open a link in a new window using Applet


Posted in : Core Java Posted on : October 30, 2010 at 6:00 PM Comments : [ 0 ]

This section contains the detail about the Opening a link in a new window using Applet in java.

Open a link in a new window using Applet

Example given in this section demonstrate about how to open a particular webpage from an applet in a new window using showDocument() with second argument as "_blank" :

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
public class AppletNewWindow 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){
	      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");
	      }
	      catch (MalformedURLException e){
	         System.out.println(e.getMessage());
	      }
	   }

}
To embed this code in web browser, we use below html code :
<html>
<title>Event Handling</title>
<hr>
<applet code="AppletNewWindow.class" 
width="500" height="100">
</applet>
<hr>
</html>

Output :

After opening it in browser :

When we click on button, it will redirect us to "www.google.com" :

The above screen is captured during page redirecting or before page complete loading.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics