Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Created June 15, 2015 09:23
Show Gist options
  • Select an option

  • Save bugabinga/750b0bfa4ba9b8e0414f to your computer and use it in GitHub Desktop.

Select an option

Save bugabinga/750b0bfa4ba9b8e0414f to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class ToolbarCustomExample2 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" );
/*
* Extending the default ToolBar has the benefit, that you inherit useful features, like auto-collapse
* (try resizing the window to something small).
*/
final ToolBar toolBar = new ToolBar();
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. */
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 );
/* It might be harder to propagate some properties: */
final int spacing = 8;
toolBar.setPadding( new Insets( 0, spacing, 0, spacing ) );
leftSection.setSpacing( spacing );
centerSection.setSpacing( spacing );
rightSection.setSpacing( spacing );
toolBar.getItems().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