Saturday 23 June 2018

JavaFX: Hyperlink widget

HyperLink widget is used to format the text as hyper links.

HyperLink class provides below constructors to create HyperLink widget.
public Hyperlink()
public Hyperlink(String text)
public Hyperlink(String text, Node graphic)

In this post, I am going to show you the usage of first two constructors, next post, I am going to show how to add an image to HyperLink widget using third constructor.

Example
Hyperlink gmail = new Hyperlink();
gmail.setText("GMAIL");

Hyperlink google = new Hyperlink("GOOGLE");

Find the below working application.

HyperLinkApp.java

package com.sample.demos;

import java.awt.Desktop;

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

public class HyperLinkApp extends Application {

 @Override
 public void start(Stage primaryStage) throws Exception {
  Hyperlink gmail = new Hyperlink();
  gmail.setText("GMAIL");
  gmail.setOnAction((ActionEvent e) -> {
   try {
    Desktop.getDesktop().browse(java.net.URI.create("https://gmail.com"));
   } catch (Exception e1) {
    System.out.println("URL clicked, but unable to open");
   }
  });
  gmail.setFont(new Font("Arial", 30));

  Hyperlink google = new Hyperlink("GOOGLE");
  google.setOnAction((ActionEvent e) -> {
   try {
    Desktop.getDesktop().browse(java.net.URI.create("https://google.com"));
   } catch (Exception e1) {
    System.out.println("URL clicked, but unable to open");
   }
  });
  google.setFont(new Font("Arial", 30));

  VBox vBox = new VBox(20, gmail, google);

  Scene scene = new Scene(vBox);

  primaryStage.setScene(scene);

  primaryStage.setTitle("Hyper link widget Example");
  primaryStage.setWidth(500);
  primaryStage.setHeight(400);
  primaryStage.show();
 }

}

TestFx.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(HyperLinkApp.class, args);
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment