Java Program to go to a Link using Applet

«
»

This is a Java Program to go to a Link using Applet

Problem Description

We have to write a program in Java such that it creates an applet where the user has got choices of two links. When the user clicks on either of the buttons, the link is opened in the default web browser.

Expected Input and Output

For opening a link using applet, we can have the following 3 different sets of input and output.

advertisement

1. To view the applet :

When the program is executed,
it is expected that the applet contains two button - google and yahoo.

2. When the user selects Google :

If the user selects the button labeled "google",
then it is expected that the default google page is opened in the browser.

3. When the user selects Yahoo :

advertisement
If the user selects the button labeled "yahoo",
then it is expected that the default yahoo page is opened in the browser.
Problem Solution

1. Create an applet with the two buttons.
2. Add ActionListener to the buttons.
3. Form the complete URL of the page.
4. Use the Desktop class of java.awt package to open the link.

Program/Source Code

Here is source code of the Java Program to open a link using an applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to Go To a Link using Applet */
  2. import java.awt.*;
  3. import java.applet.*;
  4. import java.net.*;
  5. import java.awt.event.*;
  6. import java.awt.Desktop.*;
  7. public class Goto_Link extends Applet implements ActionListener
  8. {
  9.     //Function to initialize the applet
  10.     public void init()
  11.     {
  12. 	setBackground(Color.white);
  13. 	Button button1 = new Button("google");
  14. 	Button button2 = new Button("yahoo");
  15. 	this.add(button1);
  16. 	this.add(button2);
  17. 	button1.addActionListener(this);
  18. 	button2.addActionListener(this);
  19.     }
  20.     //Function to go to the link
  21.     public void actionPerformed(ActionEvent e)
  22.     {
  23. 	String button = e.getActionCommand();
  24. 	String link = "http://www."+button+".com";
  25. 	try
  26. 	{
  27. 	    Desktop.getDesktop().browse(new URI(link));
  28. 	}
  29. 	catch (Exception E)
  30. 	{
  31. 	}
  32.     }
  33. }
  34. /*
  35. <applet code=Goto_Link.class width=500 height=500>
  36. </applet>
  37. */

To compile and execute the program use the following commands :

advertisement
>>> javac Goto_Link.java
>>> appletviewer Goto_Link.java
Program Explanation

1. To form a complete link prefix “http://www.” before the domain and suffix “.com” after it.
2. Use Desktop class to handle the URI (Uniform Resource Identifier)
file.
3. Use method browse(URI) to open the link in the default browser of the system.

Runtime Test Cases

Here’s the run time test cases for opening a link using an applet.

Test case 1 – To View the Applet
java-applet-goto-link-default

advertisement

Test case 2 – Go To Google (When the user selects Google)
java-applet-goto-link-google

Test case 3 – Go To Yahoo (When the user selects Yahoo)
java-applet-goto-link-yahoo

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn