Opening a URL from an Applet

This is the example of opening a url in same window from an applet.

Opening a URL from an Applet

This is the example of opening a url in same window from an applet.

Opening a URL from an Applet

Opening a URL from an Applet

     

Introduction

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

In this program you will see that many features have been used for opening url from an applet. This program is using two functions in which one is init() and another is actionPerformed(ActionListener ae). Function init() is self executed when the program is loaded on the browser. In the init() function a button labeled "Google" is created and it's action is performed using the statement b.addActionListener(this) which passes the object. And all the actions are performed in the another one function actionPerformed(ActionEvent ae) which gets the object ae of the ActionEvent class. Object ae tells you about the source of the event through ae.getSource() which is stored in the Button type source variable after changing the type of the retrieved event-source object and source.getLabel() gives you the caption of the button.

AppletContext has been used in this program. In fact AppletContext is the interface defines the methods, that allow an applet to interact with the context in which it runs (which is usually a Web browser or an applet viewer). And getAppletContext() is used to the reference for the AppletContext interface. To get the AppletContext of the applet getAppletContext() function can be used. We have used the function  getAppletContext.showDocument(u,"_self") that loads the web page linked to the url. In the first case the applet page will be replaced. In the second case, _self is the target which can be :

  1. "_self"   - show in current frame
  2. "_parent"   - show in parent container
  3. "_top"  - show in topmost frame
  4. "_blank"    - show in a new, top-level window
  5. "string"  - show in a frame with that string name.
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;

public class tesURL 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 u = new URL(link);
//  a.showDocument(u,"_blank");
//  _blank to open page in new window  
  a.showDocument(u,"_self");
  }
  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?