I'm currently working with JavaFX' Text
and TextFlow
layout, and I need to figure out how to center the Text
node inside a TextFlow
.
As you see in the picture below, I've added some ImageView
's, to simulate emoticons which I want to add.
The problem is, that they are aligned differently. While the emoticons are centered, the text stays at the bottom.
The green border line represents the TextFlow
's border, the blue border line the Text
's one.
I've already tried out to set the Text's textOrigin
property to CENTER
, but it doesn't change anything in my case. Setting textAlignment
to CENTER
won't work either.
Here's my code excerpt:
public CChatMessage(String senderName, String messageText)
{
this.sender = new Label(senderName);
this.sender.setTextAlignment(TextAlignment.CENTER);
this.sender.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 14));
this.message = new Text(messageText);
this.message.setTextAlignment(TextAlignment.CENTER);
this.message.setTextOrigin(VPos.CENTER);
this.setEffect(new DropShadow());
this.setAlignment(Pos.CENTER);
this.setPadding(new Insets(0, 10, 10, 10));
TextFlow messagePane = new TextFlow();
messagePane.setStyle("-fx-border-color: green");
messagePane.setTextAlignment(TextAlignment.CENTER);
Image smileyImage = new Image("/resources/smiley.png");
messagePane.getChildren().addAll(this.message, new ImageView(smileyImage), new ImageView(smileyImage), new ImageView(smileyImage),
new ImageView(smileyImage), new ImageView(smileyImage), new ImageView(smileyImage));
if(!senderName.equals(""))
{
CChatMessage.setMargin(messagePane, new Insets(10, 0, 0, 0));
this.message.setFont(Font.font("Calibri", FontWeight.SEMI_BOLD, 18));
this.getChildren().addAll(this.sender, messagePane);
}
else
{
this.setPadding(new Insets(5, 5, 5, 5));
message.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 11));
this.getChildren().add(messagePane);
}
}