Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Applet Java - Opening a url in new window from an applet

Related Tutorials


 
 

Share on Google+Share on Google+

Java - Opening a url in new window from an applet

Advertisement
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

     

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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

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:2
eyrjh
March 28, 2012
h

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()); } } }
pradeep
August 12, 2012
better code

import javax.swing.*; import java.awt.event.*; import java.awt.*; public class frame1 extends JFrame{ public frame1() { setTitle("pradeep's"); setSize(200,100); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { frame1 ex = new frame1(); JButton google = new JButton("Google"); google.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent d) { try { Runtime rTime = Runtime.getRuntime(); String url= "www.google.com"; String browser ="GIVE PATH OF YOUR BROWSER HERE WITH A SPACE AT END ,(EX:"C:/Users/pradeep/AppData/Local/Google/Chrome/Application/chrome.exe ") "; Process pc= rTime.exec(browser + url); pc.waitFor(); } catch(Exception e){ System.out.println(e); }}}); ex.add(google); } }); } }
DMCA.com