The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 5 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Problem Description:
How to goto a link using Applet?
Solution:
Following example demonstrates how to go to a particular webpage from an applet using showDocument() method of AppletContext class.
import java.applet.*; import java.awt.*; import java.net.*; import java.awt.event.*; publicclass tesURLextendsAppletimplementsActionListener{ publicvoid init(){ String link="yahoo"; Button b=newButton(link); b.addActionListener(this); add(b); } publicvoid actionPerformed(ActionEvent ae){ Button src=(Button)ae.getSource(); String link="http://www."+src.getLabel()+".com"; try{ AppletContext a= getAppletContext(); URL u=new URL(link); a.showDocument(u,"_self"); } 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.
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- Th-----------ank----------- Yo-----------u f-----------or -----------usi-----------ng -----------our----------- we-----------bsi-----------te -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill-----------