JavaFX - Hyperlink



Hyperlinks are UI components that allow users to navigate to a web page or perform an action when clicked. They are similar to buttons but have different appearances and behavior. For example, we can find hyperlinks of any web page inside the address bar located at the top of display window as shown in the below figure −

Hyperlinks

Creating Hyperlink in JavaFX

In JavaFX, the hyperlink is represented by a class named Hyperlink. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a hyperlink in JavaFX. Constructors of the Hyperlink class are listed below −

  • Hyperlink() − It is the default constructor that constructs a hyperlink without label text.

  • Hyperlink(String str) − It creates a new hyperlink with the specified label text.

  • Hyperlink(String str, Node icons) − It creates a new hyperlink with the specified text and graphics labels.

In JavaFX, the hyperlinks are created in two forms namely text and image. While creating a hyperlink, our first step would be instantiating the Hyperlink class by using any of the above mentioned constructors. Then, specify the action that should be performed when the user clicks the link. To do so, we need to add an EventHandler to its setOnAction() method. Furthermore, this EventHandler will call the specified method which helps in navigation.

By changing the implementation of setOnAction() method, we can create hyperlinks for both local resources as well as those resources that are available on a remote server.

Example

In the following JavaFX program, we will create a hyperlink as a text. Save this code in a file with the name HyperlinkExample.java.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
public class HyperlinkExample extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label labelText = new Label("On clicking the below text, it will redirect us to tutorialspoint");
      // Create a hyperlink with text
      Hyperlink textLink = new Hyperlink("Visit TutorialsPoint");
      // Set the action of the hyperlink
      textLink.setOnAction(new EventHandler() {
         @Override
         public void handle(ActionEvent event) {
            // Open the web page in the default browser
            getHostServices().showDocument("https://www.tutorialspoint.com/index.htm");
         }
      });
      // Create a scene with the hyperlink
      VBox root = new VBox(labelText, textLink);
      root.setAlignment(Pos.CENTER); 
      Scene scene = new Scene(root, 400, 300);
      // Set the title and scene of the stage
      stage.setTitle("Hyperlink in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkExample

Output

When we execute, the above program will generate the following output.

Hyperlink Output

Creating Hyperlink as an Image

To create hyperlink with an image, instantiate the ImageView class and pass its obejct as a parameter value to the constructor of Hyperlink class as shown in the next example. Save this code in a file with the name HyperlinkImage.java.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class HyperlinkImage extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label labelText = new Label("On clicking the below image, it will redirect us to tutorialspoint");
      // Create a hyperlink with image
      Image image = new Image("tutorials_point.jpg");
      ImageView imageV = new ImageView(image);
      imageV.setFitWidth(150);
      imageV.setFitHeight(150);
      Hyperlink imageLink = new Hyperlink("visit: ", imageV);
      // Set the content display position of the image
      imageLink.setContentDisplay(ContentDisplay.RIGHT);
      // Set the action of the hyperlink
      imageLink.setOnAction(new EventHandler() {
         @Override
         public void handle(ActionEvent event) {
            // Open the web page in the default browser
            getHostServices().showDocument("https://www.tutorialspoint.com/index.htm");
         }
      });
      // Create a scene with the hyperlink
      VBox root = new VBox(labelText, imageLink);
      root.setAlignment(Pos.CENTER); 
      Scene scene = new Scene(root, 400, 300);
      // Set the title and scene of the stage
      stage.setTitle("Hyperlink in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt using the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkImage.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkImage

Output

On executing the above code, it will generate the following output.

Hyperlink Image
Advertisements