OpenWebPageApplet.java
Step 1 : Initialization
- The ‘init()’ method is called when the applet loads. It initializes the user interface elements in this procedure.
- It constructs a “GO” button, a text field with a pre-filled value, and ties the “GO” button to an action listener.
Java
public void init() { btngo = new Button( "GO" ); add(btngo); txturl = new TextField( "Enter domain like 'google'" , 20 ); add(txturl); btngo.addActionListener( this ); } |
Step 2 : Button Click Event Handling
- The ‘actionPerformed()’ method is called when the user presses the “GO” button. When the “GO” button is detected as the action event’s source, this method opens the URL.
- It retrieves the user’s input from the text field inside the ‘actionPerformed()’ method.
- If “.com” is not already present, it is appended to the input.
- Builds a ‘URL’ object from the user’s input.
- Opens the URL in the system’s default web browser after determining whether surfing is supported by the system using the ‘Desktop’ class.
Java
// Java Program to implement Button for Event Handling public void actionPerformed(ActionEvent ae) { if (ae.getSource() == btngo) { try { // Extract the user input from the text field String userInput = txturl.getText().trim(); // Append ".com" to the input if not already present if (!userInput.endsWith( ".com" )) { userInput += ".com" ; } // Construct a URL from the user's input URL url = new URL( "http://" + userInput); // Check if the system supports browsing and open the URL in the default browser if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(url.toURI()); } } catch (Exception e) { e.printStackTrace(); // Handle exceptions } } } |
Step 3: Handling errors
The code is enclosed in a try-catch block to deal with potential errors, such as problems with the system’s default browser or invalid URLs.
When this applet is included in an HTML page, a straightforward user interface with a text field and a button is produced. The “GO” button attempts to launch the specified domain in the user’s default web browser when the user types in a domain name (such as “google”) and clicks it. In the absence of “.com,” the applet adds it to the domain name before attempting to open the URL. The URL is opened in the browser using the ‘Desktop’ class, and any problems are handled correctly thanks to exception handling.
The final OpenWebPageApplet.java file will look like this:
Java
// Java Applet Program to Handle Errors import java.applet.Applet; import java.awt.Button; import java.awt.Desktop; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; public class OpenWebPageApplet extends Applet implements ActionListener { Button btngo; TextField txturl; public void init() { btngo = new Button( "GO" ); add(btngo); txturl = new TextField( "Enter domain like 'google'" , 20 ); add(txturl); btngo.addActionListener( this ); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == btngo) { try { String userInput = txturl.getText().trim(); // If the user didn't include .com, add it if (!userInput.endsWith( ".com" )) { userInput += ".com" ; } URL url = new URL( "http://" + userInput); if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(url.toURI()); } } catch (Exception e) { e.printStackTrace(); } } } } |
Running the Applet in Visual Studio Code with Java 8
- Java 8 is required in order for our applet to function properly. Verify that Java 8 is the chosen runtime for your project in Visual Studio Code before continuing.
- In Visual Studio Code, select “Java Project”.
- Click the “ellipsis” or “more options” symbol, which is represented by three vertical dots.
- Click the drop-down menu and select “Configure Java Runtime.”
- Select ‘Configure Java Runtime’ from the following screen and select the location where Java 8 is installed. Choose the relevant folder.
Compilation
With the correct runtime set up, the next step is compiling our Java file to generate the necessary class file. Open a terminal within Visual Studio Code.
Run the following command to compile the `OpenWebPageApplet.java` file using the Java 8 compiler:
& "C:\Program Files\Java\jdk1.8.0_202\bin\javac" appletdemo\OpenWebPageApplet.java
Once the command executes successfully, you’ll find a `OpenWebPageApplet.class` file generated in your directory.
Navigating to the Applet Directory:
Move to the `appletdemo` directory by running the following command in the terminal:
cd appletdemo
Running the Applet:
With everything set up and our Java file compiled, it’s time to run our applet and see it in action.
Use the following command to run `OpenWebPageApplet.html` using the Java applet viewer:
& "C:\Program Files\Java\jdk1.8.0_202\bin\appletviewer" -J"-Djava.security.policy=my_policy.txt" OpenWebPageApplet.html
How to Open a Link in a New Window Using Applet?
A Java applet is a little application created in the Java programming language and run on a web browser while embedded in an HTML page. In essence, it’s a method for introducing Java’s “write once, run anywhere” feature to the world of web browsers.
Components and Organization
- Applets are subclasses of the ‘java.applet.Applet’ class.
- For their graphical user interface, they can use either the AWT (Abstract Window Toolkit) or Swing frameworks, however, AWT has a smaller environmental impact and was previously more popular.