Manu
Manu

Reputation: 4137

JavaFX colspan not working for Text in GridPane

I am trying to center a label spanning two columns of a GridPane. However, it does not seem to work.

Application class:

public class GUIMainFXML extends Application {
@Override
public void start(Stage primaryStage) {
    try {

        GridPane p = (GridPane) FXMLLoader.load(getClass().getResource(
                "FirstGUIexample.fxml"));

        Scene scene = new Scene(p);

        scene.getStylesheets().add(
                getClass().getResource("application.css").toExternalForm());

        primaryStage.setTitle("Welcome (FXML CODE)");
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}}

fxml file:

<GridPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
hgap="5" vgap="5">
<padding>
    <Insets top="10" right="10" bottom="10" left="10" />
</padding>
<children>
    <Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.rowIndex="0" />
    <Button text="Press me" GridPane.halignment="CENTER"
        GridPane.rowIndex="1" GridPane.columnSpan="2" />

    <CheckBox text="Don't touch me" GridPane.rowIndex="2"
        GridPane.halignment="CENTER" GridPane.columnSpan="2" />

    <Label text="Type me: " GridPane.halignment="CENTER"
        GridPane.columnIndex="1" GridPane.rowIndex="3" />

    <TextField GridPane.rowIndex="3" GridPane.columnIndex="2"
        GridPane.halignment="CENTER" />
</children>
</GridPane>

application.css

Text {
    -fx-font-size: 15pt;
    -fx-font-family: Tahoma;
    -fx-font-weight: bold;
}

I would like to set it either in the css or in the fxml, I am just testing.

Upvotes: 2

Views: 5700

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36792

You haven't provided the columnIndex for Text and Button, which you want to align in the center.

<Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
    GridPane.columnIndex="1" GridPane.rowIndex="0" id="argh" />
<Button text="Press me" GridPane.halignment="CENTER" GridPane.columnIndex="1"
    GridPane.rowIndex="1" GridPane.columnSpan="2"  />

Upvotes: 4

Related Questions