Boos34
Boos34

Reputation: 1

Text not appearing in JavaFX

I'm trying to display the score for a game I am making but for some reason the text won't appear once added to the group.

Here is my code:

Text scoreText = new Text(scoreString);

scoreText.setFont(new Font("ARIAL", 30);
scoreText.setStyle("-fx-font-weight: bold;");
scoreText.setFill(Color.WHITE);

Pane scorePane = new Pane(scoreText);

scoreText.relocate(100, 100);

root.getChildren().add(scoreText);

Upvotes: 0

Views: 2331

Answers (1)

James_D
James_D

Reputation: 209320

You are trying to add scoreText to two different parents: once here:

Pane scorePane = new Pane(scoreText);

which makes scoreText a child of scorePane, and once more here:

root.getChildren().add(scoreText);

which makes scoreText a child of root. Since a node cannot appear twice in the scene graph, this just won't work.

If you want scoreText to be wrapped in a pane, add it to the pane and add the pane to root:

Text scoreText = new Text(scoreString);

// ...

Pane scorePane = new Pane(scoreText);

scoreText.relocate(100, 100);

root.getChildren().add(scorePane);

If you don't need it in the pane, then just add it directly to root:

Text scoreText = new Text(scoreString);

// ...

// omit this:
// Pane scorePane = new Pane(scoreText);

scoreText.relocate(100, 100);

root.getChildren().add(scoreText);

Upvotes: 1

Related Questions