Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom border style in JavaFX CSS?

Tags:

css

javafx

I would like to create border style similar to predifened "dashed" style (-fx-border-style: dashed).

How to create dashed border in CSS with custom lengths of dash segments, line cap and line join?

like image 433
krsi Avatar asked May 23 '15 03:05

krsi


1 Answers

See the JavaFX CSS reference for Region, in particular the possible values for -fx-border-style. You can use segments(...) to define arbitrary line segment lengths: there are also settings for line-cap (square, butt, or round) and line-join (miter, bevel, or round).

Quick example:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class CustomBorderExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Region region = new Region();
        region.getStyleClass().add("custom-dashed-border");
        region.setMinSize(400, 400);
        StackPane root = new StackPane(region);
        root.setPadding(new Insets(16));
        Scene scene = new Scene(root, 480, 480);
        scene.getStylesheets().add("custom-dashed-border.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

with

custom-dashed-border.css:

.custom-dashed-border {
    -fx-border-color: blue ;
    -fx-border-width: 5 ; 
    -fx-border-style: segments(10, 15, 15, 15)  line-cap round ;
}

which gives

enter image description here

like image 50
James_D Avatar answered Sep 30 '22 20:09

James_D