Open In App

JavaFX | Hyperlink Class

Improve
Improve
Like Article
Like
Save
Share
Report

Hyperlink class is a part of JavaFX. A Hyperlink is an HTML type label which can contain text and graphic or both. It responds to rollovers and clicks. When a hyperlink is clicked/pressed isVisited() becomes true. A Hyperlink behaves similarly to a Button. When a hyperlink is pressed and released an ActionEvent is sent. Due to which your application can perform some action based on this event.

Constructors of the class:

  1. Hyperlink(): Creates a hyperlink with no text or graphic.
  2. Hyperlink(String t): Creates a hyperlink with specified text as its label.
  3. Hyperlink(String t, Node g): Creates a hyperlink with specified text and graphic as its label.

Commonly Used Methods:

Method Explanation
isVisited() Returns true if the hyperlink is not yet visited else false.
setVisited(boolean v) Sets the value of the property visited.
setOnAction(EventHandler v) Sets the value of the property onAction.
fire() Implemented to invoke the ActionEvent if one is defined.

Below programs illustrate the use of Hyperlink class:

  1. Java Program to create a hyperlink and add it to the stage also add an event handler to handle the events: This program creates a Hyperlink indicated by the name hyperlink. The Hyperlink will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the hyperlink is visited or not. The function setTitle() is used to provide title to the stage. Then an HBox is created, on which addChildren() method is called to attach the hyperlink and label inside the scene. Finally, the show() method is called to display the final results. We would create an event handler to handle the button events. The event handler would be added to the hyperlink using setOnAction() function.




    // Java Program to create a hyperlink and add
    // it to the stage also add an event handler
    // to handle the events
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
      
    public class hyperlink extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            // set title for the stage
            stage.setTitle("creating hyperlinks");
      
            // create a hyperlink
            Hyperlink hyperlink = new Hyperlink("hyperlink");
      
            // create a HBox
            HBox hbox = new HBox();
      
            // create a label
            Label label = new Label("hyperlink not visited");
      
            // action event
            EventHandler<ActionEvent> event = 
            new EventHandler<ActionEvent>() {
      
                public void handle(ActionEvent e)
                {
                    label.setText("hyperlink visited ");
                }
            };
      
            // when hyperlink is pressed
            hyperlink.setOnAction(event);
      
            // add hyperlink
            hbox.getChildren().add(hyperlink);
            hbox.getChildren().add(label);
      
            // create a scene
            Scene scene = new Scene(hbox, 200, 200);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:


  2. Java Program to create a hyperlink with both text and image on it and also add an event handler to it: This program creates a Hyperlink indicated by the name hyperlink with an image and text on it. The image will be included using the File Input Stream that imports the image. Then we will create an image using the object of the file input stream and then create an image view using the image file. The Hyperlink will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the hyperlink is visited or not. The function setTitle() is used to provide title to the stage. Then an HBox is created, on which addChildren() method is called to attach the hyperlink and label inside the scene. Finally, the show() method is called to display the final results. We would create an event handler to handle the button events. The event handler would be added to the hyperlink using setOnAction() function.




    // Java Program to create a hyperlink with 
    // both text and image on it and also add 
    // an event handler to it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import java.io.*;
    import javafx.scene.image.*;
      
    public class hyperlink_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("creating hyperlinks");
      
                // create a input stream
                FileInputStream input = new FileInputStream("f:\\gfg.png");
      
                // create a image
                Image image = new Image(input);
      
                // create a image View
                ImageView imageview = new ImageView(image);
      
                // create a hyperlink
                Hyperlink hyperlink = new Hyperlink("GeeksforGeeks", imageview);
      
                // create a HBox
                HBox hbox = new HBox();
      
                // create a label
                Label label = new Label("hyperlink not visited");
      
                // action event
                EventHandler<ActionEvent> event =
                 new EventHandler<ActionEvent>() {
      
                    public void handle(ActionEvent e)
                    {
                        label.setText("hyperlink visited ");
                    }
                };
      
                // when hyperlink is pressed
                hyperlink.setOnAction(event);
      
                // add hyperlink
                hbox.getChildren().add(hyperlink);
                hbox.getChildren().add(label);
      
                // create a scene
                Scene scene = new Scene(hbox, 200, 200);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  3. Note: The above programs might not run in an online IDE. Please use an offline compiler.

    Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Hyperlink.html



    Last Updated : 04 Sep, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads