Murkan Fusa
Murkan Fusa

Reputation: 31

JavaFx - How to use all vertical height (width) in the HBox with labels

I need to design a java application using javaFx. In the fxml there is two hBoxes; one filled with text fields and one filled with labels. Every text fields and label hGrow set to ALWAYS but text fields do grow horizontaly but labels wont.

Note that I know I can set a Pref Width to 999999 on all of them and they will behave as I want, but there has to be a more legit way.

Here is the fxml:

<VBox prefHeight="375.0" prefWidth="640.0">
   <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
      <TextField promptText="ichi" HBox.hgrow="ALWAYS" />
      <TextField promptText="ni  " HBox.hgrow="ALWAYS" />
      <TextField promptText="san " HBox.hgrow="ALWAYS" />
   </HBox>
   <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
      <Label text="eins" HBox.hgrow="ALWAYS" />
      <Label text="zwei" HBox.hgrow="ALWAYS" />
      <Label text="drei" HBox.hgrow="ALWAYS" />
   </HBox>
</VBox>

As you can see all the hgrows set to ALYAWS but the results are like this: HBoxes; one filled with text fields, one filled with labels. Label one doesnt grow on horizontal

Trying to fit all labels in to HBox with equal width distrubition. I figured a way but it is cheeky

Upvotes: 0

Views: 678

Answers (1)

SedJ601
SedJ601

Reputation: 13859

Key Code:

Max Width = MAX_VALUE
Hgrow = ALWAYS

Full code:

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?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/19" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
         <children>
            <TextField maxWidth="1.7976931348623157E308" promptText="ichi" HBox.hgrow="ALWAYS" />
            <TextField maxWidth="1.7976931348623157E308" promptText="ni" HBox.hgrow="ALWAYS" />
            <TextField maxWidth="1.7976931348623157E308" promptText="san" HBox.hgrow="ALWAYS" />
         </children>
      </HBox>
      <HBox VBox.vgrow="ALWAYS">
         <children>
            <Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="eins" HBox.hgrow="ALWAYS" />
            <Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="zwei" HBox.hgrow="ALWAYS" />
            <Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="drei" HBox.hgrow="ALWAYS" />
         </children>
      </HBox>
   </children>
</VBox>

Output:

enter image description here

Upvotes: 1

Related Questions