More specifically, why are my JavaFX controls not being centered? Here are two screenshots, the first just after starting (I moved the window into a more visible spot but have not yet resized it), and the second is just after resizing it to show off my problem. Bonus points if you help me ensure it's properly sized (on all DPIs) when it first shows:
Conveniently, the relevant code is included in those screenshots. If you still need it as text, here you go:
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, 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, 0, 1);
}
{
downloadButton = new Button("Get it!");
downloadButton.setAlignment(Pos.CENTER);
rootGrid.add(downloadButton, 0, 2);
}
holder.setMinimumSize(new Dimension((int)(rootGrid.getPrefWidth() + .5), (int)(rootGrid.getPrefHeight() + .5)));
setMinimumSize(holder.getMinimumSize());
}
To center a stage the screen call stage. centerOnScreen().
You need to set alignment for your GridPane as alignment="center" and all elements will be aligned in center. Update You can align your buttons in center like this.
Use a layout pane that can center the label, and let the label be its "preferred size" (i.e. just big enough to hold the text), or. Make the label fill the entire width of its container, and set its alignment property to center.
You can set the margin for child nodes of a JavaFX VBox using the static setMargin() method. Here is an example of setting the margin around a JavaFX Button using the setMargin() method: Button button = new Button("Button 1"); VBox vbox = new VBox(button); VBox. setMargin(button, new Insets(10, 10, 10, 10));
Place your controls in a VBox (or other similar root layout pane) and set the VBox alignment to center.
This is my personal advice on starting with layout in JavaFX (it's just advice and not applicable to everybody, you can take it or leave it):
Hi-dpi support
See the answer to:
You can load the following up in SceneBuilder to easily display it:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox 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>
</VBox>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With