Friday 22 June 2018

JavaFX: Create Separator widget

Separator class provides below constructors to create separator widget.

public Separator()
public Separator(Orientation orientation)

Ex
Separator separator1 = new Separator();
separator1.setMaxWidth(80);
separator1.setHalignment(HPos.LEFT);

Separator separator2 = new Separator(Orientation.VERTICAL);
separator2.setMaxWidth(80);
separator2.setHalignment(HPos.LEFT);

Find the below working application.

SeparatorApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SeparatorApp extends Application {

 @Override
 public void start(Stage primaryStage) throws Exception {
  final VBox vBox = new VBox();

  final String[] days = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday" };
  final Label[] labels = new Label[days.length];

  for (int i = 0; i < days.length; i++) {
   labels[i] = new Label(days[i]);
  }

  Separator separator1 = new Separator();
  separator1.setMaxWidth(80);
  separator1.setHalignment(HPos.LEFT);

  Separator separator2 = new Separator();
  separator2.setMaxWidth(80);
  separator2.setHalignment(HPos.LEFT);

  vBox.getChildren().addAll(labels);
  vBox.getChildren().add(3, separator1);
  vBox.getChildren().add(6, separator2);

  vBox.setSpacing(5);

  primaryStage.setScene(new Scene(vBox));

  primaryStage.setTitle("Separator widger Example");
  primaryStage.setWidth(900);
  primaryStage.setHeight(500);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

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





Previous                                                 Next                                                 Home

No comments:

Post a Comment