-1

I need to restrict node's children size. My parent node is a VBox. I tried to set maxWidth, but it doesn't work (wide children are out of VBox bound)

How can I restrict children size to parent? Is there any way apart from binding?

Expected behaviour: Children maxWidth is automatically the same as VBox (without setting it manually)

EDIT:

My code (fxml):

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.layout.HBox?>
<HBox prefWidth="800" prefHeight="400">
    <VBox maxWidth="50" style="-fx-background-color: blue;">
        <Text text="looooooooooooooooooooooooooooooooooooooooooooooong one"/>
    </VBox>
</HBox>
  • same procedure as always: [mcve] please ;) it's the layout that decides how to size its children, have a look at the java doc and then choose which/how to use it – kleopatra Jun 06 '20 at 12:16
  • Have you tried `VBox.vgrow`? Also, take in consideration wrapping your `VBox` inside and `HBox`. Anyways, a lot of the view layer behaviours can easily be set just by modying the `fxml` and not the associated controller – gabriel garcia Jun 06 '20 at 12:16
  • @gabrielgarcia Just added some description. I only care about the width – ImNotARobot Jun 06 '20 at 12:27
  • please read the reference help page and act accoringly – kleopatra Jun 06 '20 at 12:55

1 Answers1

0

There's a wrappingWidth property in javafx.scene.text.Text which you can use to limit the size of your Text node.

[EDIT] After some research, I've found some answers and documentation regarding this question:

Note that this approach doesn't work with SceneBuilder (reason: there's no way to do this with Scene Builder since it doesn't support any mechanism for inserting <fx:define> blocks in your FXML, among other things)

<HBox fx:id="hbox" prefHeight="400" prefWidth="800" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <fx:define>
            <Screen fx:factory="getPrimary" fx:id="screen" />
        </fx:define>
        <VBox fx:id="vbox" maxWidth="${screen.width}" HBox.hgrow="always"  style="-fx-background-color: blue;">
            <Text text="looooooooooooooooooooooooooooooooooooooooooasdasdsadasdsadasdasdooooong one" wrappingWidth="${vbox.width*0.10}"/>
        </VBox>
    </children>
</HBox>

This should do the trick for you, based on the links given above, but unfortunately I can't test it right now.

gabriel garcia
  • 368
  • 3
  • 17
  • I can put every single node here and it will not be sized to VBox maxWidth. I don't want to set everyting manually, I'd like to have solution that enables maxWidth inheritance or something like that. – ImNotARobot Jun 06 '20 at 12:37
  • So you are looking for a generic solution for your components size? for example, the ability to set their sizes as a percentage of their parent's? – gabriel garcia Jun 06 '20 at 12:40
  • I'd like to set a maxWidth to my parent that will affect all children of my parent, e.g. they will have the same maxWidth – ImNotARobot Jun 06 '20 at 12:42
  • Ok, that's a very common asked feature when working with JavaFX. Just give me a sec and I will try to give you a working solution to your problem. – gabriel garcia Jun 06 '20 at 12:46