How to create a separator using JavaFX?


A separator is a horizontal or, the vertical line separating the UI elements of an application.

In JavaFX the javafx.scene.control.Separator class represents a separator, to create a separator you need to instantiate this class. This class has three properties namely −

  • halignment − This property specifies the horizontal alignment of a vertical separator. You can set value to this, using the setHalignment() method

  • orientation − This property specifies the orientation of the current separator i.e. horizontal or vertical. You can set value to this property using the setOrientation() method.

  • valignment − This property specifies the vertical alignment of a horizontal separator. You can set value to this, using the setValignment() method

By default the separator class creates a horizontal separator, to create a vertical separator you need to change its orientation using the setOrientation() method.

Example

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class SeparatorExample extends Application {
   public void start(Stage stage) {
      //Creating the check boxes
      CheckBox checkBox1 = new CheckBox("Hindi");
      CheckBox checkBox2 = new CheckBox("Gujarathi");
      CheckBox checkBox3 = new CheckBox("Punjabi");
      CheckBox checkBox4 = new CheckBox("Telugu");
      CheckBox checkBox5 = new CheckBox("Tamil");
      CheckBox checkBox6= new CheckBox("Malayalam");
      //Creating a label
      Label label = new Label("Select known languages:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating a separator
      Separator sep = new Separator();
      sep.setMaxWidth(80);
      sep.setHalignment(HPos.CENTER);
      //Adding the check boxes and separator to the pane
      VBox vBox = new VBox(5);
      vBox.setPadding(new Insets(5, 5, 5, 50));
      vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3,
      checkBox4, checkBox5, checkBox6);
      //Adding the separator after the 3rd check box
      vBox.getChildren().add(4, sep);
      //Setting the stage
      Scene scene = new Scene(vBox, 595, 200, Color.BEIGE);
      stage.setTitle("Seperator Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 18-May-2020

683 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements