Ossama
Ossama

Reputation: 2433

how to center an image in javafx

How can I move the facebook picture so that it is vertically centered to the text??

Code:

GridPane footerPane = new GridPane();     
double size = 15;

TextFlow textFlow = new TextFlow();
Text text1 = new Text("Get prayer time notifications and daily hadith on your mobile by following us on Facebook or Twitter");
text1.setFont(Font.font("Tahoma", size));
text1.setFill(Color.GRAY);
ImageView facebook = new ImageView(new Image(getClass().getResourceAsStream("/Images/facebook.png")));
Text text5 = new Text(".");
text5.setFont(Font.font("Tahoma", size));

textFlow.getChildren().addAll(text1, facebook, text5);
footerPane.setConstraints(footer_Label, 0, 0);
footerPane.getChildren().add(textFlow);

Image:

enter image description here

Upvotes: 1

Views: 1187

Answers (2)

Cephalopod
Cephalopod

Reputation: 15145

Just make your image fit the text size and then use setScaleY to increase its size and setTranslateY to adjust its position:

double textSize = 12, iconSize = 32;
ImageView iv = new ImageView( ... );
iv.setFitHeight(textSize);
iv.setScaleY(iconSize/textSize);
// iv.setTranslateY(   )

Result:
Example

Upvotes: 3

Hendrik Ebbers
Hendrik Ebbers

Reputation: 2600

I have the same issue with TextFlow. I think TextFlow currently doesn't support a aligment for its children.

Upvotes: 2

Related Questions