Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add text in Rectangle in Javafx

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!

like image 398
fredsilva Avatar asked Aug 24 '14 23:08

fredsilva


1 Answers

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);
like image 87
ROMANIA_engineer Avatar answered Nov 07 '22 17:11

ROMANIA_engineer