- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to open a link in a new window using Applet in Java
Problem Description
How to open a link in a new window using Applet?
Solution
Following example demonstrates how to go 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 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) {
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());
}
}
}
Result
The above code sample will produce the following result in a java enabled web browser.
View in Browser.
java_applets.htm
Advertisements