Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align a button in Java FX toolbar

I am building a UI using Java FX scene builder and I want a button in a toolbar to float towards the right side of the toolbar. I have tried changing the node orientation of the parent(toolbar) and also the button but both seem to be ignored.

like image 560
Japheth Ongeri - inkalimeva Avatar asked Jul 22 '14 19:07

Japheth Ongeri - inkalimeva


People also ask

How do I create a JavaFX ToolBar?

You can create a toolbar by instantiating the javafx. scene. control. ToolBar class.

Do something when button is pressed JavaFX?

To make a button do something in JavaFX you need to define the executable code usually by using a convenience method like setOnAction() . Then, when the button is clicked, JavaFX does the heavy lifting of fetching that pre-defined code, and executing it on the JavaFX Application thread.


2 Answers

Add a pane with no content which always grows to fit available space between the left aligned tools in the bar and right aligned ones.

tool

<?xml version="1.0" encoding="UTF-8"?>  <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?>  <ToolBar prefHeight="40.0" prefWidth="318.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">    <Button text="Apples" />    <Button text="Oranges" />    <Pane HBox.hgrow="ALWAYS" />    <Button text="Help" /> </ToolBar> 
like image 150
jewelsea Avatar answered Sep 18 '22 10:09

jewelsea


(Verified in Scene Builder 11.0.0)

One option: Use two ToolBar containers wrapped with an HBox container. Put the Help button in the right ToolBar. Put the left-aligned buttons in the left toolbar. For the left ToolBar, set the HGrow to ALWAYS. For the Right ToolBar, set HGrow to NEVER. For each ToolBar, set all Sizes to USE_COMPUTED_SIZE.

Here is the relevant fxml:

<HBox VBox.vgrow="NEVER">   <children>     <ToolBar HBox.hgrow="ALWAYS">       <items>         <Button text="Apples" />         <Button text="Oranges" />       </items>     </ToolBar>     <ToolBar HBox.hgrow="NEVER">       <items>         <Button text="Help" />       </items>     </ToolBar>   </children> </HBox> 

This is the hierarchy as displayed in Scene Builder:

Scene Builder Hierarchy

This is the display within Scene Builder:

Result in Scene Builder

like image 24
MichaelShimniok Avatar answered Sep 20 '22 10:09

MichaelShimniok