JavaFX: Align Buttons inside ButtonBar (using SceneBuilder or fxml)

本小妞迷上赌 提交于 2019-12-07 06:44:50

问题


I have a JavaFX ButtonBar with two Buttons (created via SceneBuilder).

I want one of the buttons to be left-aligned and the other right-aligned. (see screenshot)

From the docs I already know how I could achieve this inside the java-source-code:

ButtonBar.setButtonData(newButton, ButtonData.LEFT);

BUT

I want to know how to achieve this WITHOUT having to write this inside my java-files but
how I can achieve this using just SceneBuilder or the corresponding fxml file.

My .fxml file currently looks like this:

<ButtonBar>
  <buttons>
    <Button text="New" />
    <Button text="Save" />
  </buttons>
</ButtonBar>

* I'm on Windows
** This answer is not what I want, because he is using a ToolBar, but I want to know how to do this with a ButtonBar (and his approach does not work for the ButtonBar)


回答1:


After some Trial and Error I found at least a way to do it directly via the .fxml-file:

You can assign the Button elements with ButtonBar.buttonData attributes and then assign a value to them.

<ButtonBar>
  <buttons>
    <Button text="New" ButtonBar.buttonData="LEFT" />
    <Button text="Save" ButtonBar.buttonData="RIGHT" />
  </buttons>
</ButtonBar>

The docs for the ButtonBar.ButtonData enum are pretty straighforward. So I found the solution to my problem with the "LEFT" and "RIGHT" enum values.

STILL

If anyone knows/finds out how to do this directly in SceneBuilder, I would be grateful!




回答2:


There is the "Transforms" property in Layout tab of the button on the right side of Scene Builder. Change "Translate X". (The position of the button will change horizontally.) "Translate Y" will change the button's position vertically. You need to calculate the position first.

I just found it.




回答3:


Via the Inspector, usually on the right side in NetBeans, you will find the tab "Layout". Open it and scroll to the section "Transform".

The option "Translate X" is for adjusting the buttons in the buttonbar at the x-axis.



来源:https://stackoverflow.com/questions/32426848/javafx-align-buttons-inside-buttonbar-using-scenebuilder-or-fxml

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