Reto Mayer
Reto Mayer

Reputation: 55

JavaFx layout missbeheavior with prefered size and min Size

Disclaimer: I am a JavaFx beginner and come from the wpf world

I have a problem with the behavior of the prefered height and the max height. The min height is ignored and the prefered height is used. What do i wrong?

Code Example: (this is only to illustrate the problem)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<VBox fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane minHeight="100.0" style="-fx-background-color: lightblue;" VBox.vgrow="ALWAYS" />
      <Pane fx:id="buttonPane" style="-fx-background-color: lightgreen;">
         <children>
            <Button minHeight="0.0" minWidth="0.0"  onAction="#buttonPaneClick" prefHeight="200.0" text="Button" />
         </children>
      </Pane>
   </children>
</VBox>

enter image description here

Problem: the Button is only half showed and not scaled down. But the min height is 0 so i would expectd that the button goes to 0 when not more place is available but he is cropped out.

Upvotes: 0

Views: 1459

Answers (1)

James_D
James_D

Reputation: 209330

From the Pane documentation:

This class may be used directly in cases where absolute positioning of children is required since it does not perform layout beyond resizing resizable children to their preferred sizes.

(my emphasis).

So the Pane containing the button will resize it to its preferred size no matter what. If you replace the pane containing the button with a layout container that does more layout than this (e.g. a HBox) then you will see the behavior you are expecting:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane minHeight="100.0" style="-fx-background-color: lightblue;" VBox.vgrow="ALWAYS" />
      <HBox fx:id="buttonPane" style="-fx-background-color: lightgreen;">
         <children>
            <Button minHeight="0.0" minWidth="0.0"  prefHeight="200.0" text="Button" />
         </children>
      </HBox>
   </children>
</VBox>

Upvotes: 1

Related Questions