JavaFX Hyperlink looks like a hyperlink on a web page, but it is not. The hyperlink on a web page is used to navigate to another web page, while the hyperlink in JavaFX is different because you are free to perform anything using the action event.
How to use the JavaFX Hyperlink
JavaFX Hyperlink is a control that is simply a button styled to look like a hyperlink on a web page. The hyperlink has the same appearance as the hyperlink on a webpage, it can have focus, and it does have a dashed rectangular border when it has focus. When you hover over the hyperlink, the cursor changes to a hand-type cursor and its text will change to underline.
It contains a visited property of BooleanProperty type. When the hyperlink is activated or clicked for the first time, it is considered a “visited,” and the visited property is set to true automatically. The color is different whether the hyperlink is visited or not. The following example will show you a demonstration of how we use the hyperlink control in JavaFX.
JavaFX Hyperlink Example
This tutorial will teach you how to use the JavaFX Hyperlink—for example, creating the hyperlink control in JavaFX and adding the hyperlink to our layout. The primary example in this tutorial is to show you how to open a web page using the JavaFX WebView and Desktop class in JavaFX.
Create the Hyperlink
It is effortless to create a JavaFX Hyperlink. We need to make an object or instantiate the hyperlink constructor. Please proceed to the example below to learn more about creating the hyperlink control in JavaFX.
// Create the hyperlink Hyperlink hyperlink = new Hyperlink("Hyperlink");
Adding to layout
We need to use the getChildren() method to add a hyperlink to the layout in JavaFX. If you are unfamiliar with the layout in JavaFX, you can learn more at the given link and proceed below to learn more about adding the hyperlink control in JavaFX.
StackPane root = new StackPane(); root.getChildren().add(hyperlink);
Open a website using Hyperlink and WebView
We can do whatever we want in the JavaFX Hyperlink because we are free to perform any action in the ActionEvent handler. This example will teach you how to open a URL from the Hyperlink control and display the webpage on our WebView. Please proceed below to learn more about opening a URL in the JavaFX WebView.
//Lets create an Action Event in our Hyperlink hyperlink.setOnAction(event ->{ WebView view = new WebView(); view.getEngine().load("https://kensoftph.com"); });
Output
Open a website using Hyperlink and Desktop class
The Desktop class is different, and we can open a URL using the Desktop class in the default browser in our application. If you are unfamiliar with the Desktop class, you can learn more about using the class, and the link is given above. The following example below shows how to open a URL in JavaFX using the Desktop class. Please proceed below to learn more about it. Again, we will use the ActionEvent to perform the command.
// Open a URL using the Desktop class in JavaFX hyperlink.setOnAction(event ->{ Desktop desktop = Desktop.getDesktop(); desktop.browse(java.net.URI.create("https://google.com")); });