-1

I'm currently making a little JavaFX application. In this application, I created a Label and wanted to stretch itself in both directions. The Label is placed inside a GridPane and I tried all Methods I know, including the Double.MAX_VALUE Method, which in my opinion doesn't seem to be very elegant and also worked sometimes in a false way. Below you see the result I get:

enter image description here

And this is my code:

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        GridPane gp = new GridPane();
        gp.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(3))));

        Label label = new Label("Some text that could be longer");
        label.setFont(new Font(20));
        label.setTextFill(Color.WHITE);
        label.setBackground(new Background(new BackgroundFill(Color.rgb(34, 35, 36), CornerRadii.EMPTY, Insets.EMPTY)));
        label.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(3))));


        // Commands that don't look like to work
        GridPane.setHgrow(label, Priority.ALWAYS);
        GridPane.setFillWidth(label, true);
        GridPane.setVgrow(label, Priority.ALWAYS);
        GridPane.setFillHeight(label, true);

        ColumnConstraints cc = new ColumnConstraints();
        cc.setFillWidth(true);
        cc.setHgrow(Priority.ALWAYS);
        cc.setPercentWidth(100);

        RowConstraints rc = new RowConstraints();
        rc.setFillHeight(true);
        rc.setVgrow(Priority.ALWAYS);
        rc.setPercentHeight(100);
        // end


        gp.add(label, 0, 0);
        gp.getColumnConstraints().add(cc);
        gp.getRowConstraints().add(rc);


        primaryStage.setScene(new Scene(gp, 800, 300));
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

I don't know what to do else, because none of the Methods above worked for me. In multiple examples, they didn't even worked once.

Alex
  • 39
  • 1
  • 10
  • You also have to make the label cover more than one column. – SedJ601 Apr 07 '21 at 14:07
  • See: `void add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan)` from [here](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/GridPane.html). – SedJ601 Apr 07 '21 at 14:09
  • 1
    @Sedrick In the sample code, there's only one column, so there's no need to make the label span multiple columns. Presumably the OP just wants it to fill its cell in the grid horizontally. – James_D Apr 07 '21 at 14:13
  • @James_D, Thanks! I didn't notice. – SedJ601 Apr 07 '21 at 15:23
  • If the `GridPane` only has one row and column, I would suggest using a different Parent Node. – SedJ601 Apr 07 '21 at 15:27

1 Answers1

3

By default, a Label has its maxWidth set to Region.USE_PREF_SIZE, so it cannot grow wider than its preferred size. Set the maxWidth to be unbounded using

label.setMaxWidth(Double.MAX_VALUE);

Among your other settings, you only need to set the hgrow to ALWAYS, which you can do either with the static method, or via the column constraints:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        GridPane gp = new GridPane();
        gp.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(3))));

        Label label = new Label("Some text that could be longer");
        label.setFont(new Font(20));
        label.setTextFill(Color.WHITE);
        label.setBackground(new Background(new BackgroundFill(Color.rgb(34, 35, 36), CornerRadii.EMPTY, Insets.EMPTY)));
        label.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(3))));
        
        label.setMaxWidth(Double.MAX_VALUE);


        GridPane.setHgrow(label, Priority.SOMETIMES);

        // Can use the following instead of the previous line:

        // ColumnConstraints cc = new ColumnConstraints();
        // cc.setHgrow(Priority.ALWAYS);
        // gp.getColumnConstraints().add(cc);


        gp.add(label, 0, 0);

        primaryStage.setScene(new Scene(gp, 800, 300));
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Ok I understand, that makes sense. But why do the commands in my code don't do exactly this ? (The lines between the two comments) – Alex Apr 07 '21 at 14:26
  • 1
    @Alex Because the grid pane won't size the label bigger than the max width. (That's what "max width" means.) – James_D Apr 07 '21 at 14:29
  • Also, when I set a maxHeight or maxWidth to a parent pane, it iss just getting ignored and a child with a Double.MAX_VALUE gets outside. Thats another Problem that I can't handle – Alex Apr 07 '21 at 14:30
  • 1
    layouts are free to size their children to whatever they deem okay - pref/max/min are just hints. What each does, is explained exactly in its java doc :) – kleopatra Apr 07 '21 at 14:45
  • 1
    @Alex I don't understand what that means. The size of the parent pane is controlled by *it's* parent, attempting to respect its min, pref, and max sizes. I suggest you reread the [layout documentation](https://openjfx.io/javadoc/15/javafx.graphics/javafx/scene/layout/package-summary.html) – James_D Apr 07 '21 at 14:45