parsecer
parsecer

Reputation: 5106

JavaFx: Label text doesn't wrap

I have a BorderPane with Panes for left, right and center elements and HBox for top element. On the right Pane there is a Label. The Label contains some text, which doesn't wrap and therefore gets cut.

public class Main extends Application implements EventHandler<ActionEvent> {
    private static BorderPane gamePane;
    private static Pane cellsPane;
    private static HBox buttonsPane;
    private static Pane statsPane;
    private static Pane setupsPane;

    Label cellsCountLabel;    

    static int  gameWidth= 700;
    static int gameHeight=600;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Hello World");

        gamePane = new BorderPane();

        buttonsPane = new HBox(5);
        statsPane = new Pane();
        cellsPane = makeGrid(20);
        setupsPane = new Pane();

        cellsPane.setStyle("-fx-background-color:  #ffffff; -fx-border-color: black");
        buttonsPane.setStyle("-fx-background-color:  #f2f2f2; -fx-border-color: black");
        statsPane.setStyle("-fx-background-color:  #f2f2f2; -fx-border-color: black");
        setupsPane.setStyle("-fx-background-color:  #f2f2f2; -fx-border-color: black");

        cellsPane.setMaxWidth(400);
        statsPane.setMaxWidth((gameWidth-400)/2);
        setupsPane.setMaxWidth((gameWidth-400)/2);

        createCellButton = new Button();
        deleteCellsButton = new Button();

        createCellButton.setText("Create a cell");
        deleteCellsButton.setText("Delete cells");

        ...

        buttonsPane.setSpacing(10);
        buttonsPane.setPadding(new Insets(10, 10, 10, 10));
        buttonsPane.getChildren().add(createCellButton);
        buttonsPane.getChildren().add(deleteCellsButton);

        gamePane.setTop(buttonsPane);
        gamePane.setCenter(cellsPane);

        ...

        cellsCountLabel = new Label("Cells Count: " + (cellId + 1));
        statsPane.getChildren().add(cellsCountLabel);
        gamePane.setLeft(statsPane);

        setupsPane.getChildren().add(new Label("Setups Panel1111115552222222133331111"));
        gamePane.setRight(setupsPane);

        gamePane.setMargin(statsPane, new Insets(0, 0, 0, 0));
        gamePane.setMargin(cellsPane, new Insets(0,0,0,0));
        gamePane.setMargin(setupsPane, new Insets(0,0,0,0));

        statsPane.setPrefWidth(150);
        setupsPane.setPrefWidth(150);

        cellsCountLabel.setWrapText(true);  //doesn't work

        Scene scene = new Scene(gamePane, gameWidth, gameHeight);
        primaryStage.setScene(scene);

        ...

        primaryStage.show();
}
...
}

However, there exists space between left, center and right Panes. The space gets smaller as the content in those Panes get larger, however, when it gets too large it messes with the center Pane.

enter image description here

I tried using cellsCountLabel.setWrapText(true), but it didn't work.

Upvotes: 3

Views: 10251

Answers (1)

Matt
Matt

Reputation: 3187

Try this

Label label = new Label("Setups Panel1111115552222222133331111");
label.setWrapText(true);
label.setMaxWidth((gameWidth-400)/2);
setupsPane.getChildren().add(label);

Also if you go this route you may want to declare something like this

static double sidePaneWidth = (gameWidth-400)/2;

for later use

You can also make this into a function like so if necessary

private Label newSidePaneLabel(String labelString){
    Label label = new Label(labelString);
    label.setWrapText(true);
    label.setMaxWidth(sidePaneWidth);
    return label;
}

Upvotes: 3

Related Questions