JavaFX 8 - Positioning Text Vertical Center of HBox


Question

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!

1
16
10/15/2014 1:41:06 AM

Accepted Answer

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);
38
10/15/2014 5:41:44 AM

If you are using SceneBuilder, select your HBox and in the Inspector section, change the

Properties : Node : Alignment

value to CENTER_LEFT:

enter image description here


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