4

How can I align the items of a toolbar to the right?? All alignment settings seem to not work.

I´m using the layout designer.

Please help

enter image description here

<BorderPane fx:controller="vmanager.ui.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
        prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
    <ToolBar prefWidth="200.0" BorderPane.alignment="CENTER_RIGHT">
        <items>
            <Button alignment="CENTER_RIGHT" mnemonicParsing="false" text="_"/>
            <Button mnemonicParsing="false"/>
            <Button mnemonicParsing="false" text="X" onAction="#close"/>
        </items>
    </ToolBar>
</top>

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136

3 Answers3

4

This is doable with an AnchorPane instead of the toolbar:

<BorderPane id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <top>
    <AnchorPane prefHeight="50.0">
      <children>
        <Button layoutY="14.0" mnemonicParsing="false" text="Button" AnchorPane.rightAnchor="14.0" />
        <Button layoutY="15.0" mnemonicParsing="false" text="Button" AnchorPane.rightAnchor="70.0" />
        <Label layoutY="18.0" prefWidth="449.0" text="Label" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="140.0" />
      </children>
    </AnchorPane>
  </top>
</BorderPane>

I also added the "window title" label. You can style the AnchorPane however you like later, if this achieves your desired layout behaviour.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
1

You cannot align Toolbar items to right in Scenebuilder. For this to achieve you have to do it programatically. Below is the sample code I use on top of a TableView

Platform.runLater(new Runnable() {
@Override
public void run() {
    btnset.setMinWidth(toolbar.getWidth()/2);
    btnset.setAlignment(Pos.CENTER_LEFT);
    recnum.setMinWidth(toolbar.getWidth()/2);
    recnum.setAlignment(Pos.CENTER_RIGHT);
}
});

This can be done only after the ToolBar has been painted on the scene, hence using Platform.runlater.

enter image description here

Sudip Saha
  • 300
  • 2
  • 6
  • 21
0

What if you used an HBox rather than a Toolbar? I know you don't get the look of the toolbar, but it is easy to float things to the right using a regular Pane with the Hgrow attribute set to ALWAYS as a spacer. Here (total hack), I put a Toolbar behind the HBox in a StackPane to let the look of it come through, but you could certainly do that better with css. Also, you will, or course, need id's on your buttons in order to work with them. I also didn't bring forward your embedded controller definition, so don't forget to include that.

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <StackPane BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
         <children>
            <ToolBar prefHeight="40.0" prefWidth="200.0" />
            <HBox prefHeight="40.0" prefWidth="200.0">
               <children>
                  <Pane maxWidth="1.7976931348623157E308" HBox.hgrow="ALWAYS" />
                <Button mnemonicParsing="false" text="_" />
                  <Button mnemonicParsing="false" text="O" />
                  <Button mnemonicParsing="false" text="X" />
               </children>
               <padding>
                  <Insets bottom="6.0" left="6.0" right="6.0" top="6.0" />
               </padding>
            </HBox>
         </children>
      </StackPane>
   </top>
</BorderPane>
RonSiven
  • 939
  • 2
  • 10
  • 23
  • 2
    See marked duplicate Ron, it's likely a better solution. – jewelsea Apr 04 '17 at 06:59
  • That's cool. I didn't know you could inject attributes into the FXML like that. Is there a way to add the HBox.hgrow attribute from within Scenebuilder? I just stuffed it into the code, and it (obviously) worked great. – RonSiven Apr 04 '17 at 14:53