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.

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 :

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*;
  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 :

>>> javac
>>> appletviewer
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)
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

Test case 2 – Go To Google (When the user selects Google)

Test case 3 – Go To Yahoo (When the user selects Yahoo)


Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.