Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 8 - Positioning Text Vertical Center of HBox

I am having a very difficult time positioning Text in an HBox. I am able to set the Horizontal Alignment of the Text, but I am not able to set the Vertical Alignment of the Text. I am trying to set the Text in the Vertical Center of the Hbox, however it is appearing at the top.

Any help would be much appreciated.

// Create The Updates Progress Bar At The Bottom Of The Window
HBox checkUpdatesBar = new HBox();
checkUpdatesBar.setId("checkUpdatesBar");
checkUpdatesBar.setPrefSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
checkUpdatesBar.setMinSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
checkUpdatesBar.setMaxSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);

// Create The 'Check For Updates...' Text
Text checkForUpdates = new Text();
checkForUpdates.setFont(Font.font("Arial Black", FontWeight.BLACK, 15));
checkForUpdates.setWrappingWidth(CHECK_PANE_WIDTH / 2.8);
checkForUpdates.setFill(Color.WHITE);
checkForUpdates.setTextAlignment(TextAlignment.CENTER);
checkForUpdates.setText("Checking For Updates ...".toUpperCase());

checkUpdatesBar.getChildren().add(checkForUpdates);

My code produces the following: http://puu.sh/ccEkp/41a26038a4.png

I like my Horizontal Positioning. I know its not in the middle because I set it to be that way. However, I just care about the Vertical Positioning.

I have tried the following separately:

checkForUpdates.setLayoutY(20);
checkForUpdates.setY(20);

I chose the number 20 because I wanted to move the Text 20 pixels down from the top of the HBox. I calculated this value in Photoshop. Though if there is a better way for setting the Text to the Vertical Center of the HBox, please do enlighten me.

Thanks again for all the help!

like image 989
Sameer Avatar asked Oct 15 '14 01:10

Sameer


People also ask

How do you center something in HBox?

If you just want to position the text in the center of the HBox , you can use the alignment property of the HBox to do it. Save this answer.

What is HBox in JavaFX?

The JavaFX HBox component is a layout component which positions all its child nodes (components) in a horizontal row. The Java HBox component is represented by the class javafx. scene.


1 Answers

If you just want to position the text in the center of the HBox, you can use the alignment property of the HBox to do it.

Set the alignment as CENTER_LEFT, to place the children, starting for the CENTER of the HBox's vertical space

checkUpdatesBar.setAlignment(Pos.CENTER_LEFT);
like image 64
ItachiUchiha Avatar answered Sep 19 '22 18:09

ItachiUchiha