1

I can't really understand how this stretching works in JavaFX. I have my prefWidth to Infinity both for the HBox and the TextField, so the TextField should be larger if we resize the frame. Please help me, what am I missing here. Thank you.

<GridPane fx:id="first" hgap="5" vgap="5"  xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" >

<columnConstraints>
    <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" />
</columnConstraints>

<VBox.margin>
    <Insets left="10.0" top="15"/>
</VBox.margin>
<HBox GridPane.columnIndex="0" GridPane.rowIndex="0" prefWidth="Infinity"  maxWidth="Infinity">

    <fx:define>
        <ToggleGroup fx:id="myToggleGroup"/>
    </fx:define>
    <children>
        <RadioButton text="System" toggleGroup="$myToggleGroup">
        </RadioButton>
        <RadioButton text="Document" toggleGroup="$myToggleGroup">
            <HBox.margin>
                <Insets left="200.0"/>
            </HBox.margin>
        </RadioButton>
    </children>
</HBox>
<HBox GridPane.rowIndex="1" GridPane.columnIndex="0"  >
    <Label text="Name:" minWidth="50">
    </Label>

    <TextField maxWidth="Infinity" minWidth="450" prefWidth="Infinity">
        <HBox.margin>
            <Insets left="20.0"/>
        </HBox.margin>
    </TextField>
</HBox>

Joey
  • 83
  • 1
  • 13
  • See if http://stackoverflow.com/questions/35438104/javafx-alignment-of-label-in-gridpane (which is different, but has a full explanation of how layout in grid panes works) helps. – James_D Feb 23 '16 at 16:35
  • Also I am only allowed to use fxml in this project. – Joey Feb 23 '16 at 16:37
  • Doesn't matter, everything in that question can also be done with FXML. (Though, "*only* allowed to use FXML"??? You cannot write an application with only FXML, you need Java in the controller.) – James_D Feb 23 '16 at 16:38
  • Yeah, I meant the GUI only. – Joey Feb 24 '16 at 08:30

1 Answers1

1

By default, an HBox will assign controls their preferred sizes. I'm not sure how they will handle a preferred size of "Infinity" (I get some errors from the layout calculations with this): I would remove all the prefWidth="Infinity" and just tell the HBox to allow the text field to grow:

<TextField HBox.hgrow="ALWAYS" maxWidth="Infinity">

Here is a complete example (slightly modified to fit into a SSCCE):

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

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.geometry.Insets?>

<GridPane fx:id="first" hgap="5" vgap="5"
    xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">

    <columnConstraints>
        <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" />
    </columnConstraints>

    <HBox GridPane.columnIndex="0" GridPane.rowIndex="0">

        <fx:define>
            <ToggleGroup fx:id="myToggleGroup" />
        </fx:define>
        <children>
            <RadioButton text="System" toggleGroup="$myToggleGroup">
            </RadioButton>
            <RadioButton text="Document" toggleGroup="$myToggleGroup">
                <HBox.margin>
                    <Insets left="200.0" />
                </HBox.margin>
            </RadioButton>
        </children>
    </HBox>
    <HBox GridPane.rowIndex="1" GridPane.columnIndex="0">
        <Label text="Name:" minWidth="50">
        </Label>

        <TextField HBox.hgrow="ALWAYS" maxWidth="Infinity">
            <HBox.margin>
                <Insets left="20.0" />
            </HBox.margin>
        </TextField>
    </HBox>
</GridPane>

and the code to run it:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("layout.fxml"))));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • On a sidenote, how could I achieve that my second radiobutton always stays in the middle if I stretch? – Joey Feb 24 '16 at 08:59