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:
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.