How to replicate this 2-1 layout in JavaFX?

soyfroggy :

I've looked into all of the layout options in the docs, and I am not allowed to use FXML. I don't understand where to start with configuring the 3 containers in the scene. enter image description here

James_D :

There are many ways you can do this; probably the most immediate is to put the two panes on the left in a VBox, and then use a BorderPane, with the VBox on the left and the "CCTV" pane in the center. This looks like:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LayoutExample extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane lighting = new Pane(); 
        lighting.getChildren().add(new Label("Lighting"));
        Pane heating = new Pane();
        heating.getChildren().add(new Label("Heating"));
        Pane cctv = new Pane();
        cctv.getChildren().add(new Label("CCTV"));

        lighting.getStyleClass().add("control-pane");
        heating.getStyleClass().add("control-pane");
        cctv.getStyleClass().add("control-pane");

        BorderPane root = new BorderPane() ;
        VBox left = new VBox();
        left.getChildren().add(lighting);
        left.getChildren().add(heating);

        // expand both panes in left to full width of vbox:
        left.setFillWidth(true);
        // add vertical space between panes:
        left.setSpacing(5);
        // allocate extra vertical space equally to both panes in left:
        VBox.setVgrow(lighting, Priority.ALWAYS);
        VBox.setVgrow(heating, Priority.ALWAYS);

        root.setLeft(left);
        root.setCenter(cctv);

        // Add a left margin to the center pane to give it some space:
        BorderPane.setMargin(cctv, new Insets(0, 0, 0, 10));

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("layout-style.css").toExternalForm());

        primaryStage.setScene(scene);

        primaryStage.setWidth(800);
        primaryStage.setHeight(640);
        primaryStage.show();

    }

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

}

This CSS isn't quite right, but gives you an idea how to add the borders:

.control-pane {
  -fx-background-color: -fx-body-color, -fx-outer-border, -fx-body-color ;
  -fx-background-insets: 0, 1, 2 ;
  -fx-background-radius: 0, 1, 0 ;
  -fx-padding: 3 ;

  /** Just to demo empty boxes: **/
  -fx-min-width: 300px ;
  -fx-min-height: 300px ;
 }

This looks like:

enter image description here

You could also use a HBox as the root, and set the hgrow on each to prioritize extra horizontal space on the CCTV pane. Or use a GridPane, and set the row span of the CCTV pane to 2. There are probably many other ways to do this.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23206&siteId=1