How to centralize JavaFX controls
More specifically, why is my JavaFX control not centered? Here are two screenshots. The first one is after the beginning (I moved the window to a more obvious position, but haven't resized yet), and the second one is after adjusting it to show my problem If you help me confirm its size (on all DPI), reward points when it is displayed for the first time:
Conveniently, the relevant code is included in those screenshots If you still need it as text, go to:
private void initJFXPanel(JFXPanel holder) { { { rootGrid = new GridPane(); rootGrid.setAlignment(Pos.CENTER); rootGrid.setPadding(new Insets(16)); rootGrid.setHgap(16); rootGrid.setVgap(8); } interior = holder.getScene(); if (interior == null) holder.setScene(interior = new Scene(rootGrid)); interior.setRoot(rootGrid); } { statusLabel = new Label("Checking for Updates..."); statusLabel.setAlignment(Pos.CENTER); statusLabel.setTextAlignment(TextAlignment.CENTER); rootGrid.add(statusLabel,0); } { progressBar = new ProgressBar(); progressBar.setProgress(-1); progressBar.setPrefWidth(Constants.MAX_WIN_BOUNDS.width / 5d); // 1/5 the width of the screen rootGrid.add(progressBar,1); } { downloadButton = new Button("Get it!"); downloadButton.setAlignment(Pos.CENTER); rootGrid.add(downloadButton,2); } holder.setMinimumSize(new Dimension((int)(rootGrid.getPrefWidth() + .5),(int)(rootGrid.getPrefHeight() + .5))); setMinimumSize(holder.getMinimumSize()); }
Solution
solution
Place control in V@R_105_2419 @(or other similar root layout pane), put set the VBox alignment in the center
Layout recommendations
This is my personal suggestion to start layout in JavaFX (this is only a suggestion and does not apply to everyone. You can accept or leave it):
>Your window and all controls and layouts will automatically resize to the preferred size. > Let the built-in layout manager and layout system calculate for you as much as possible, just provide the minimum layout prompt required to obtain the results. > Stick to using only JavaFX or swing code until you are familiar with the two code development styles and really need to mix them. > Use the scenebuilder tool to use different layouts and be familiar with JavaFX layout mechanism. > Study JavaFX layout tutorial. > Review presentation on JavaFX layout. > To move the center of the screen to stage centerOnScreen(). > Consider using java 8u40's built-in dialog support (when publishing)
Hi DPI support
See the answer:
> JavaFX 8 HiDPI Support
Fxml based dialog box example code
You can easily display it by loading the following in scenebuilder:
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <V@R_105_2419@ alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="8.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <Label fx:id="updateStatus" text="Checking for Updates..." /> <ProgressBar fx:id="updateProgress" prefWidth="200.0" progress="0.0" /> <Button fx:id="updateAction" mnemonicParsing="false" text="Get it!" /> </children> <padding> <Insets bottom="16.0" left="16.0" right="16.0" top="16.0" /> </padding> </V@R_105_2419@>