简体   繁体   中英

Setting Labels to a GridPane in JavaFx

I have a GridPane called matrix in a FXML file, and in the controller I am adding a label to each cell of the GridPane, however I am not able to center that label in the GridPane. I have tried setting all kinds of alignments. Here is some of my code: This is all in a for loop by the way

for (int i = 0; i < label.length; i++) {
            for (int j = 0; j < label[i].length; j++) {
               label[i][j] = new Label();
               label[i][j].setTextAlignment(TextAlignment.CENTER);
               label[i][j].setAlignment(Pos.CENTER);
               label[i][j].setCenterShape(true);
               label[i][j].setText("" + randomNumber);
               matrix.add(label[i][j], j, i);
            }
}

How would I horizontally center the label in matrix cell. Please Help. Thank you!

Try

GridPane.setHalignment(label[i][j], HPos.CENTER);

( Javadocs )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM