This Applet example demonstrate how to open any URL in new browser page. The applet has Text Field in which user enters web URL and when user press Go button the specified URL will open in new browser window.
Create an applet which has a Text Field to accept a URL string, and displays the document of the URL string in a new browser window. When user enters http://www.google.com and clicks on Go button the applet will opens the url in new window and this is accomplished by getAppletContext.showDocument(url, target) method of the AppletContext.
Open_URL.Java
-----------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
Create an applet which has a Text Field to accept a URL string, and displays the document of the URL string in a new browser window. When user enters http://www.google.com and clicks on Go button the applet will opens the url in new window and this is accomplished by getAppletContext.showDocument(url, target) method of the AppletContext.
Open_URL.Java
-----------------------------------------------------------------------------------------------
/* Applet program which takes URL from user into textfield and opens the url in new page */ /* Use applet in browser to get it run*/ import java.applet.*; import java.awt.*; import java.net.*; import java.awt.event.*; public class Open_URL extends Applet implements ActionListener { String link = ""; TextField tf; public void init() { Button b = new Button("Go"); tf=new TextField("http://www.google.com"); b.addActionListener(this); add(tf); add(b); } public void actionPerformed(ActionEvent ae) { //get the link entered by user from textbox link= tf.getText(); try { AppletContext a = getAppletContext(); URL url = new URL(link); a.showDocument(url,"_blank"); // Opens the url in new window //a.showDocument(url,"_self"); _self to open url in same window } catch (Exception e) { System.out.println(e.getMessage()); } } }Applet.html
-------------------------------------------------------------------------------------------
<html>
<head>
<title> Open URL in new page Using Java Applet</title>
</head>
<body>
<applet code="Open_URL.class" height=300 weight=300></applet>
</body>
</html>
0 comments:
Post a Comment