2
votes

I fail to center a Button in a VBox in Java code! I used Scene Builder to create a SplitPane with the left AnchorPane having a Button centered in a VBox. I want to recreate this, a Button in a VBox, in the right AnchorPane but not in FXML, in Java Code. But the right Button does not center, although I use vb.setAlignment(Pos.CENTER); :

enter image description here

My FXML code:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testimageview.MainViewController">
   <children>
      <SplitPane dividerPositions="0.5" prefHeight="200.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane fx:id="leftAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <VBox alignment="CENTER" prefHeight="198.0" prefWidth="171.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <Button mnemonicParsing="false" text="FXML" />
                     </children>
                  </VBox>
               </children>
            </AnchorPane>
          <AnchorPane fx:id="rightAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
        </items>
      </SplitPane>
   </children>
</AnchorPane>

And my Java Controller class code:

package testimageview;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

public class MainViewController implements Initializable {

    @FXML
    private AnchorPane leftAnchorPane;

    @FXML
    private AnchorPane rightAnchorPane;

    public MainViewController() {
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    VBox.setVgrow(leftAnchorPane, Priority.ALWAYS);

    VBox vb = new VBox();

    Button rightButton = new Button();
    rightButton.setText("Java");

    vb.setAlignment(Pos.CENTER);
    vb.getChildren().addAll(rightButton);

    rightAnchorPane.getChildren().add(vb);

    } 
}
1
Why would you use a VBox? Why not a FlowLayout?matt
Well, it works in the FXML with Scene Builder (Left Pane). So why not? What I want to say: If it works with FXML, shouldn´t it also work with Java(right Pane)?gkane
It just seems like a FlowLayout is the one you want.matt

1 Answers

1
votes

Well you did not define the dimensions for the VBox so by default (inside a ScrollPane) it will fit the size of it's children in your case a Button size like 50,50 or something like that, so you can not see the alignment. All you need to do is to define the size of the VBox to match the size of the second AnchorPane or you can just bind their dimensions ( Width, height ) like :

vb.prefWidthProperty().bind(rightAnchorPane.widthProperty());
vb.prefHeightProperty().bind(rightAnchorPane.heightProperty());