JavaFX | Hyperlink Class
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:
- Hyperlink(): Creates a hyperlink with no text or graphic.
- Hyperlink(String t): Creates a hyperlink with specified text as its label.
- 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:
- 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 eventsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;publicclasshyperlinkextendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating hyperlinks");// create a hyperlinkHyperlink hyperlink =newHyperlink("hyperlink");// create a HBoxHBox hbox =newHBox();// create a labelLabel label =newLabel("hyperlink not visited");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){label.setText("hyperlink visited ");}};// when hyperlink is pressedhyperlink.setOnAction(event);// add hyperlinkhbox.getChildren().add(hyperlink);hbox.getChildren().add(label);// create a sceneScene scene =newScene(hbox,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:
- 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 itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjava.io.*;importjavafx.scene.image.*;publicclasshyperlink_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating hyperlinks");// create a input streamFileInputStream input =newFileInputStream("f:\\gfg.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a hyperlinkHyperlink hyperlink =newHyperlink("GeeksforGeeks", imageview);// create a HBoxHBox hbox =newHBox();// create a labelLabel label =newLabel("hyperlink not visited");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){label.setText("hyperlink visited ");}};// when hyperlink is pressedhyperlink.setOnAction(event);// add hyperlinkhbox.getChildren().add(hyperlink);hbox.getChildren().add(label);// create a sceneScene scene =newScene(hbox,200,200);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.err.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- JavaFX | Tab Class
- JavaFX | Pos Class
- JavaFX | Background Class
- JavaFX | ToolBar Class
- JavaFX | WebView Class
- JavaFX | Glow Class
- JavaFX | Slider Class
- JavaFX | TabPane Class
- JavaFX | HBox Class
- JavaFX | BorderPane Class
- JavaFX | Group Class
- JavaFX | SplitPane Class
- JavaFX | Pane Class
- JavaFX | TextFlow Class
- JavaFX | VBox Class
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
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



