JavaFX Hyperlink

JavaFX Hyperlink class can be used to display a html like hyperlink, present in web pages, in GUI window.

Also remember that, unlike webpages, it does not open a link in browser or something like that. But, it triggers an action when clicked and has properties like isVisited, if the hyperlink is already visited.

In this tutorial, we will learn how to display JavaFX Hyperlink and use different properties of JavaFX Hyperlink in your GUI application.

Following is a quick code snippet of how to create a JavaFX Hyperlink.

 Hyperlink hyperlink = new Hyperlink("www.tutorialkart.com");

You have to import javafx.scene.control.Hyperlink to use JavaFX Hyperlink.

Example 1 – JavaFX Hyperlink

In the following example JavaFX application, we have created a JavaFX Hyperlink and added it to a scene to display it on the GUI window.

JavaFxHyperlinkTutorial.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxHyperlinkTutorial extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("JavaFX Label - tutorialkart.com");
            
            //javafx hyper link
            Hyperlink hyperlink = new Hyperlink("www.tutorialkart.com");
            
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add hyper link to the tile pane
            tilePane.getChildren().add(hyperlink);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run this application and you should see a hyperlink with the string “www.tutorialkart.com” in the window, as shown below.

JavaFX Hyperlink

Example 2 – JavaFX Hyperlink with Action Listener

In the following example JavaFX application, we have created a JavaFX Hyperlink and set an action listener to it. When you click on the hyperlink, the action is triggered.

JavaFxHyperlinkTutorial2.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxHyperlinkTutorial2 extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("JavaFX Label - tutorialkart.com");
            
            //javafx hyper link
            Hyperlink hyperlink = new Hyperlink("www.tutorialkart.com");
            
            //set action listener
            hyperlink.setOnAction(e -> {
                System.out.println("Hyperlink is clicked.");
            });
            
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add hyper link to the tile pane
            tilePane.getChildren().add(hyperlink);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run this application and you should see a window with hyperlink. Click on the hyperlink and you should see the action triggered.

JavaFX Hyperlink
JavaFX Hyperlink - Set Action Listener

Example 3 – JavaFX Hyperlink – isVisited

In this example, we shall check the isVisited property.

JavaFxHyperlinkTutorial3.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxHyperlinkTutorial3 extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("JavaFX Hyperlink - tutorialkart.com");
            
            //javafx hyper link
            Hyperlink hyperlink = new Hyperlink("www.tutorialkart.com");
            System.out.println("Is Hyperlink visited: "+hyperlink.isVisited());
            
            //set action listener
            hyperlink.setOnAction(e -> {
                System.out.println("Is Hyperlink visited: "+hyperlink.isVisited());
            });
            
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add hyper link to the tile pane
            tilePane.getChildren().add(hyperlink);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run the application and in the console you will see that the value isVisited is false.

When you click the hyperlink, the isVisited is set to true.

JavaFX Hyperlink - isVisited

Conclusion

In this JavaFX Tutorial, we learned how to display hyperlink in GUI window. Also, we have seen an example, where we can set an action listener and also use isVisited property.