Sunday, June 26, 2016

78. HyperLink

With HyperLink, we can cause an action if a text is clicked. The text will be highlightened when selected.


This is used here to select a group of colors. This is comparable to a set of radio buttons.


package ex78;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Ex78 extends Application {
    
    Rectangle rect;
    Font font;
    
    @Override
    public void start(Stage stage) {

        String[] colors = {"red","green","blue",
            "palegreen","cyan","coral"};
        
        font = Font.font("Georgia",32);
        
        Hyperlink[] hyp = new Hyperlink[colors.length];
        for (int i = 0; i < colors.length; i++) {
            hyp[i] = makeHyp(colors[i]);
        }
        
        VBox vbox = new VBox(50, hyp);
        
        rect = new Rectangle(300,600,Color.WHITE);
        
        HBox root = new HBox(50, vbox, rect);
        root.setPadding(new Insets(50));
        root.setAlignment(Pos.CENTER);
        
        Scene scene = new Scene(root);
        stage.setTitle("Example 78. Hyperlink"); 

        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }

    private Hyperlink makeHyp(String color) {
        Hyperlink hyperlink = new Hyperlink(color);
        hyperlink.setFont(font);
        hyperlink.setOnAction(e -> {
            hyperlink.setVisited(false);
            rect.setStyle("-fx-fill:" + color);
        });
        return hyperlink;
    }
}

This is the output:


No comments:

Post a Comment