Java - Opening a url in new window from an applet

In this example you will learn how to open a new window from an applet. You can use the code given in this program to open any url by replacing the value of url.

Java - Opening a url in new window from an applet

In this example you will learn how to open a new window from an applet. You can use the code given in this program to open any url by replacing the value of url.

Java - Opening a url in new window from an applet

Java - Opening a url in new window from an applet

     

Introduction

In this example you will learn how to open a new window from an applet. You can use the code given in this program to open any url by replacing the value of url.

In this example our applet will open new browser window and then show you the web page. Opening a new window from applet is rather very simple task as all the necessary functions are already available.

When you click the button "google" button on the applet, 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.

Tutorials

  1. What is an Applet
  2. The Life cycle of An Applet
  3. Java Applet - Creating First Applet Example
  4. Java - Drawing Shapes Example in java
  5. Java - Drawing Shapes Example using color in java
  6. Java - Event Listeners Example in Java Applet
  7. Applet - Passing Parameter in Java Applet
  8. Opening a URL from an Applet
  9. Java - Opening a url in new window from an applet
  10. Applet is not Working
  11. Display image in the applet
  12. Applet Write Files Example
  13. Play Audio in Java Applet
  14. Security Issues with the Applet
  15. Swing Applet Example in java
  16. The Sample Banner Example in Java
  17. Clock Applet in Java
  18. HTML Document Creation
  19. Tag Parameters: The Tag
  20. The APPLET Tag in Detail
  21. Java and HTML: The Basics
  22. What Exactly is HTML?
  23. Welcome to the Internet
  24. java.applet package examples
  25. java.applet package examples
  26. What is an Applet - Java Applet Tutorial
  27. Java - Read file Applet
  28. Applet versus Application
  29. Tag Parameters: The Tag
  30. Java - Opening a url in new window from an applet
  31. Java - Opening a URL from an Applet
  32. Applet Tag Parameters,Applet Tag in HTML
  33. Applets in Java
  34. What is Applet in Java?
  35. What is Applet in Java with Example?