Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Created June 15, 2015 09:22
Show Gist options
  • Save bugabinga/54ccf8a2647dc355a762 to your computer and use it in GitHub Desktop.
Save bugabinga/54ccf8a2647dc355a762 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class ToolbarCustomExample extends Application
{
@Override
public void start( final Stage primaryStage )
{
final Button left = new Button( "left button" );
final Button left1 = new Button( "left1 button" );
final Button left2 = new Button( "left2 button" );
final Button right = new Button( "right button" );
final Button right1 = new Button( "right1 button" );
final Button right2 = new Button( "right2 button" );
final Button right3 = new Button( "right3 button" );
final Button center = new Button( "center button" );
final Button center1 = new Button( "center1 button" );
/*
* Simply use an HBox instead of a StackPane. A StackPane is used to center and stack Nodes on top of each
* other (Z-Order).
*/
final HBox toolBar = new HBox();
final HBox leftSection = new HBox( left, left1, left2 );
final HBox centerSection = new HBox( center, center1 );
final HBox rightSection = new HBox( right, right1, right2, right3 );
/* Center all sections and always grow them. Has the effect known as JUSTIFY. */
toolBar.setAlignment( Pos.CENTER );
HBox.setHgrow( leftSection, Priority.ALWAYS );
HBox.setHgrow( centerSection, Priority.ALWAYS );
HBox.setHgrow( rightSection, Priority.ALWAYS );
leftSection.setAlignment( Pos.CENTER_LEFT );
centerSection.setAlignment( Pos.CENTER );
rightSection.setAlignment( Pos.CENTER_RIGHT );
/*
* There might be some properties you might want to propagate down from the hbox container to all
* sections:
*/
toolBar.setFillHeight( true );
toolBar.setSpacing( 8 );
leftSection.spacingProperty().bind( toolBar.spacingProperty() );
centerSection.spacingProperty().bind( toolBar.spacingProperty() );
rightSection.spacingProperty().bind( toolBar.spacingProperty() );
toolBar.getChildren().addAll( leftSection, centerSection, rightSection );
left.setOnAction( event -> System.out.println( "left" ) );
right.setOnAction( event -> System.out.println( "right" ) );
center.setOnAction( event -> System.out.println( "center" ) );
final Scene scene = new Scene( toolBar );
primaryStage.setScene( scene );
primaryStage.show();
}
public static void main( final String[] args )
{
launch( args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment