Add Borders in JavaFX - Java JavaFX

Java examples for JavaFX:CSS

Description

Add Borders in JavaFX

Demo Code

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {
    Application.launch(args);/*w  w  w . j a  v  a 2s  .c om*/
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Generating Borders");
    Group root = new Group();
    Scene scene = new Scene(root, 600, 330, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);
    gridpane.setVgap(10);

    Label cssEditorLbl = new Label("CSS Editor");
    GridPane.setHalignment(cssEditorLbl, HPos.CENTER);
    gridpane.add(cssEditorLbl, 0, 0);

    // label Border View
    Label borderLbl = new Label("Border View");
    GridPane.setHalignment(borderLbl, HPos.CENTER);
    gridpane.add(borderLbl, 1, 0);

    // Text area for CSS editor
    final TextArea cssEditorFld = new TextArea();
    cssEditorFld.setPrefRowCount(10);
    cssEditorFld.setPrefColumnCount(100);
    cssEditorFld.setWrapText(true);
    cssEditorFld.setPrefWidth(150);
    GridPane.setHalignment(cssEditorFld, HPos.CENTER);
    gridpane.add(cssEditorFld, 0, 1);

    String cssDefault = "-fx-border-color: blue;\n" + "-fx-border-insets: 5;\n"
        + "-fx-border-width: 3;\n" + "-fx-border-style: dashed;\n";

    cssEditorFld.setText(cssDefault);
    ImageView imv = new ImageView();
    Image image2 = new Image(Main.class.getResourceAsStream("buttons.png"));
    imv.setImage(image2);

    final HBox pictureRegion = new HBox();
    pictureRegion.setStyle(cssDefault);
    pictureRegion.getChildren().add(imv);
    gridpane.add(pictureRegion, 1, 1);

    Button apply = new Button("OK");
    GridPane.setHalignment(apply, HPos.RIGHT);
    gridpane.add(apply, 0, 2);

    apply.setOnAction((e) -> {
      pictureRegion.setStyle(cssEditorFld.getText());
    });
    root.getChildren().add(gridpane);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

Related Tutorials