Table of contents
1.
Introduction
2.
JavaFX Hyperlink
2.1.
Constructors
2.2.
Methods
2.3.
Examples
2.3.1.
Example 1
2.3.2.
Example 2
3.
Frequently Asked Questions
3.1.
What is meant by stage in JavaFX?
3.2.
Name some layout panes of JavaFX.
3.3.
Why use JavaFX?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Hyperlink

Author Gurleen Kaur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Java GUI toolkit is called JavaFX (GUI stands for Graphical User Interface). JavaFX facilitates the development of desktop applications and video games. In this article, we will discuss the JavaFX Hyperlink. The JavaFX Hyperlink control is a piece of text that acts as a button, allowing you to program a Hyperlink to do something specific when the user clicks it. We will see more about it in this article.

JavaFX Hyperlink

JavaFX includes the Hyperlink class. A hyperlink is a label of the HTML type that may consist of text, graphics, or both. A button and a hyperlink have similar behaviours. An ActionEvent is sent each time a link is clicked and released. Therefore, based on this occurrence, your application may take some action. 

Since a Hyperlink can be configured to do something when the user clicks it, the JavaFX Hyperlink control is a text element that also serves as a button. It is similar to a web page's link. The class javafx.scene.control.Hyperlink is used to represent the JavaFX Hyperlink control.

Constructors

The following are the constructors of the class used to implement the JavaFX Hyperlink.

  • Hyperlink(): A simple hyperlink will be created without graphics or text.
  • Hyperlink(String t): A hyperlink is created with the defined text as a label.
  • Hyperlink(String t, Node g): A hyperlink is created with the text and graphics that define the label.

Methods

Examples

Two examples of implementing JavaFX Hyperlink will be defined below:

Example 1

In this example, the hyperlink is implemented along with the label.

HyperlinkFX.java:

//package name
package hyperlinkfx;
import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Hyperlink;  
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
public class HyperlinkFX extends Application {  
public static void main(String[] args) {  
launch(args);     
}   
@Override  
public void start(Stage primaryStage) throws Exception {  
    // hyperlink is created
    Hyperlink hp = new Hyperlink("http://www.codingninjas.com");  
    StackPane root = new StackPane();  
    //action after the link is clicked
    hp.setOnAction(e -> System.out.println("You clicked the link!"));  
    root.getChildren().add(hp);  
    //scene created
    Scene scene=new Scene(root,350,150);  
    primaryStage.setScene(scene);  
    // title for the stage
    primaryStage.setTitle("Example for Hyperlink");  
    primaryStage.show();  
}  
}  

 

Output:

Initially, the output is like this:

 

When the hyperlink is pressed several times, the output will be like this:

 

Example 2

In this example, the hyperlink is implemented along with the label and the image.

HyperlinkFX.java:

//package name
package hyperlinkfx; 
import java.io.FileInputStream;  
import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Hyperlink;  
import javafx.scene.image.Image;  
import javafx.scene.image.ImageView;  
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
public class HyperlinkFX extends Application {     
public static void main(String[] args) {  
launch(args);     
}  
@Override  
public void start(Stage primaryStage) throws Exception {  
    //hyperlink with label
    Hyperlink hp = new Hyperlink("CodingNinjas");  
    hp.setOnAction(e -> System.out.println("Link is clicked"));  
    //coding ninjas image
    FileInputStream input = new FileInputStream("C:\\Users\\LENOVO\\Downloads\\codingninjas-1.jpg");  
    Image img = new Image(input);  
    ImageView imgview=new ImageView(img);  
    //graphics set for the hyperlnk
    hp.setGraphic(imgview); 
    StackPane root = new StackPane(); 
    //hyperlink will be visible now
    root.getChildren().add(hp);  
    Scene scene = new Scene(root,400,200);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Example of Hyperlink");  
    primaryStage.show();  
}  
}  

 

Output:

Initially, the output is like this:

When the link is clicked, the console prints the output as many times the link is clicked, as shown below:

Frequently Asked Questions

What is meant by stage in JavaFX?

In JavaFX, a Stage is a top-level container covering a scene consisting of visual components.

Name some layout panes of JavaFX.

Some of them are HBox, VBox, StackPane, FlowPane, and many more.

Why use JavaFX?

Programmers use the Advanced Windowing Toolkit and Swing libraries to create GUI applications using the Java programming language. These Java programmers can now efficiently create GUI applications with rich content thanks to JavaFX.

Conclusion

In this article about JavaFX, we discussed Hyperlink control. Initially, we briefly introduced the JavaFX Hyperlink. Further, we saw the various ways of implementing the hyperlink, its constructors, methods, and more.

Go through our guided paths on Coding Ninjas Studio to learn and enhance concepts like Data Structures and AlgorithmsCompetitive Programming, and many more! Try the mock test series and participate in the contests on Coding Ninjas Studio to learn about your competency and coding ability. Also, to learn about companies like Uber, Amazon, Microsoft, and more, check the numerous interview experiencesproblems, and interview bundles.

Happy learning!

Live masterclass