How add text in Rectangle in Javafx


Question

How do I add a text at the center of a rectangle? I've been searching and have not found a way to do this.

See the class below:

public class Agent extends Rectangle{         

    public Agent() {
        setWidth(60);
        setHeight(60);
        setArcWidth(60);
        setArcHeight(60);                
        setFill(Color.BLUEVIOLET.deriveColor(0, 1.2, 1, 0.6));
        setStroke(Color.BLUEVIOLET);
     }    
}

Thanks!

1
6
8/24/2014 11:07:52 PM

Accepted Answer

Add the Text element and the Agent (Rectangle) element in a StackPane.

Text text = new Text("...");
...
StackPane stack = new StackPane();
stack.getChildren().addAll(agent, text);
12
8/25/2014 12:47:57 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon