2
votes

JavaFx 8 maxWidth Ignored

The maxWidth is ignored for HBox children with wider content.

For example, the center Pane below is far too large, extending under the right column (Pane):

 <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="1000.0" prefWidth="1236.0">
      <children>
           <Pane fx:id="leftPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0"/>
           <Pane fx:id="centerPane" maxHeight="10000.0" maxWidth="10000.0" minHeight="400.0" minWidth="120.0" prefHeight="10000.0" >
           </Pane>
           <Pane fx:id="rightPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0" />
      </children>
 </HBox>

There is no conflicting CSS, such as defining a percentage, or otherwise setting a content width.

How to Make maxWidth a Hard-Limit?

Is there a way to make JavaFX recompute the child pane sizes each time the child content changes, while never exceeding the maxWidth of the parent?

2

2 Answers

6
votes

From JavaFX 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. It is the application's responsibility to position the children since the pane leaves the positions alone during layout.

So, you cannot reach your goal by using basic Panes as children. Better try to use StackPanes instead:

A stackpane's parent will resize the stackpane within the stackpane's resizable range during layout.

A stackpane's unbounded maximum width and height are an indication to the parent that it may be resized beyond its preferred size to fill whatever space is assigned to it.

Take a look at this example (I added background colors to the StackPanes, since I used SceneBuilder to test the behavior) and tell me please if it satisfies your needs:

<HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="1000.0" prefWidth="1236.0" style="-fx-background-color: lightblue;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
  <children>
       <StackPane fx:id="leftPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0" style="-fx-background-color: red;" />
       <StackPane fx:id="centerPane" maxHeight="10000.0" maxWidth="10000.0" minHeight="400.0" minWidth="120.0" prefHeight="10000.0" style="-fx-background-color: white;"> 
       <children>
        <Label prefWidth="999.0" text="Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. " />
     </children></StackPane>
       <StackPane fx:id="rightPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0" style="-fx-background-color: green;" />
  </children></HBox>

You can also use other Pane or Box components instead.

Update:

The previous example makes the center Pane to dynamically adjust in function of the width of its children, fulfilling the parent Pane if needed, but never exceeding its maximum width. So, if you want the Pane to always fill the width of its parent, a big enough preferred width must be added to the center Pane.

<StackPane fx:id="centerPane" maxHeight="10000.0" minHeight="400.0" prefHeight="10000.0" prefWidth="1000.0"/>
1
votes

To make container resize automatically according to its children size use javafx constants like:

centerPane.setMinWidth(Control.USE_PREF_SIZE);