Reputation: 2198
I am a beginner at Java and I have just recently started to take a look at GUI and JavaFX. And at the moment I am stuck trying to center a text inside a GridPane and would need som help with this, and an explaination to what I am doing wrong.
This is my code:
GridPane grid = new GridPane();
grid.setMinSize(100, 100);
Text text = new Text("Hello World!");
text.setTextAlignment(TextAlignment.CENTER);
grid.add(text, 0, 1);
grid.setStyle("-fx-background-color: #D8BFD8;");
This is not working (the text does not center), so I asume I am doing something wrong. But I cant understand what I am doing wrong?
Upvotes: 2
Views: 4340
Reputation: 3789
I would use Labels instead of Text, that way you can set the horizontal alignment:
Label label = new Label("Hello World!");
gridpane.add(label, 0, 1);
GridPane.setHalignment(label, HPos.CENTER);
Upvotes: 4