We’ve collected these simple code snippets to allow you learn JavaFX.

Example 1: Hyperlink

Study the following code:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • HyperlinkChangeTextExample.java
  • HyperlinkExample.java
  • HyperlinkFontExample.java
  1. In your editor or IDE, create a file known as HyperlinkChangeTextExample.java.
  2. Then add the following code:

(a). HyperlinkChangeTextExample.java

We will start by adding some imports to this class:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class HyperlinkChangeTextExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

All Java applications have an entry point known as the main method. We will define it:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        link.setText("New Link Text");

        link.setOnAction((event) -> {
            System.out.println("Hyperlink clicked");
        });

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 300, 300);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.hyperlink;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HyperlinkChangeTextExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        link.setText("New Link Text");

        link.setOnAction((event) -> {
            System.out.println("Hyperlink clicked");
        });

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 300, 300);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
  1. Next create another file known as HyperlinkExample.java.
  2. And add the following code:

(b). HyperlinkExample.java

We will start by adding some imports to this class:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class HyperlinkExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

All Java applications have an entry point known as the main method. We will define it:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        link.setOnAction((event) -> {
            System.out.println("Hyperlink clicked");
        });

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 300, 300);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.hyperlink;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HyperlinkExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        link.setOnAction((event) -> {
            System.out.println("Hyperlink clicked");
        });

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 300, 300);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
  1. Next create another file known as HyperlinkFontExample.java.
  2. And add the following code:

(c). HyperlinkFontExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

We will need to extend our class using the extend keyword. By doing that our class can make use of inheritance to derive properties and functions defined in the parent class.

public class HyperlinkFontExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

Every Java Program must have a main method. This is the entry point of all Java applications include JavaFX. Add a main() method and inside it invoke the launch() function.

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        Font courierNewFontBold36 = Font.font("Courier New", FontWeight.BOLD, 36);

        link.setFont(courierNewFontBold36);

        link.setOnAction((event) -> {
            System.out.println("Hyperlink clicked");
        });

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 200, 200);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.hyperlink;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class HyperlinkFontExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        Font courierNewFontBold36 = Font.font("Courier New", FontWeight.BOLD, 36);

        link.setFont(courierNewFontBold36);

        link.setOnAction((event) -> {
            System.out.println("Hyperlink clicked");
        });

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 200, 200);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License