How can I align items of Toolbar to the right in JavaFX [duplicate]

两盒软妹~` 提交于 2020-01-13 13:45:32

问题


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

<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>


回答1:


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.




回答2:


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.




回答3:


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>


来源:https://stackoverflow.com/questions/22096520/how-can-i-align-items-of-toolbar-to-the-right-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!