JavaFX FlowPane hold controls

Introduction

To Create a flow pane

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

FlowPane arranges the nodes horizontally from left to right or vertically from top to bottom in the order in which they were added.

When one row or one column is filled, a new row or column is started.

We can specify the way the nodes are placed horizontally or vertically via:

  • Orientation.HORIZONTAL or
  • Orientation.VERTICAL.

We can specify the gap between the nodes in pixels.

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 w  ww . j  a va 2  s  . 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