How to make sure that some items located in the Vbox are not stretched. Let's say I have a Vbox there, and there are 2 elements, an image and a table. And how can I make the table stretch when stretching?

  • add a separate pane on which to throw elements that should not stretch - DaysLikeThis
  • and there is no separate property for elements that are in vBox? - Clool Mear

2 answers 2

I advise all the same to reconsider the layout of your items.
From experience I advise you to break up the elements into as many panels as possible, since you never know what needs to be added.
I will assume that your problem can be solved with a combination of VBox and BorderPane.

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TableView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class FxHelp extends Application { @Override public void start(Stage primaryStage) throws Exception { BorderPane borderPane = new BorderPane(); VBox vBox = new VBox(new Button("TEST")); borderPane.setTop(vBox); borderPane.setCenter(new TableView<>()); primaryStage.setScene(new Scene(borderPane)); primaryStage.setWidth(400); primaryStage.setHeight(300); primaryStage.show(); } } 

But it would be better to see your example.

    Inside VBox to control vertical stretching in FXML, the VBox.vgrow attribute is VBox.vgrow , which is specified for internal elements.

    In java-code, the "vbox-vgrow" restriction is used for this, which can be set using the static method VBox.setVgrow(Node, Priority) .

    Set the table to "ALWAYS", and to image - either "NEVER", or remove (by default - "INHERIT").

    Do not forget that the prefHeight, maxHeight, minHeight properties also affect the vertical size.

    FXML:

     <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.image.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <Pane style="-fx-background-color: green;"> <children> <ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" /> </children> </Pane> <TableView fx:id="table1" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS"> <columns> <TableColumn prefWidth="75.0" text="C1" /> <TableColumn prefWidth="75.0" text="C2" /> </columns> </TableView> </children> </VBox> 

    Java code:

      VBox.setVgrow(table1, Priority.ALWAYS); table1.setMaxHeight(Double.MAX_VALUE);