JavaFX Separator add between controls

Introduction

To Add Separator between controls in JavaFX

// Use a separator to better organize the layout.
Separator separator = new Separator();
separator.setPrefWidth(260);
    
// Add the label, separator, and text field to the flow pane.
fpRoot.getChildren().addAll(response, separator, tf);
    

Full source

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {
  //Create a label that will report the selection.
  private Label response = new Label("Menu Demo");

  public static void main(String[] args) {
    launch(args);/*from ww  w.  j av a2s . c o  m*/
  }
  public void start(Stage myStage) {    myStage.setTitle("Menus from java2s.com");

    // Use a BorderPane for the root node.
    BorderPane rootNode = new BorderPane();

    Scene myScene = new Scene(rootNode, 300, 300);

    myStage.setScene(myScene);

    // Use a separator to better organize the layout.
    Separator separator = new Separator();
    separator.setPrefWidth(260);

    // Create a flow pane that will hold both the response
    // label and the text field.
    FlowPane fpRoot = new FlowPane(10, 10);

    fpRoot.setAlignment(Pos.CENTER);

    // Create a text field and set its column width to 20.
    TextField tf = new TextField();
    tf.setPrefColumnCount(20);

    // Add the label, separator, and text field to the flow pane.
    fpRoot.getChildren().addAll(response, separator, tf);


    rootNode.setCenter(fpRoot);
    myStage.show();
  }
}



PreviousNext

Related