How to create toolbar with left, center and right sections in javaFX?

后端 未结 3 412
梦毁少年i
梦毁少年i 2021-01-13 04:08

I\'m trying to create a customized toolbar in javafx. This toolbar should be able to display controls in the center, in the left side and in the right side (three sections)

3条回答
  •  -上瘾入骨i
    2021-01-13 04:59

    Spring method for ToolBar section alignment works fine for me.

    springtool

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    public class SectionalToolbar extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            final Pane leftSpacer = new Pane();
            HBox.setHgrow(
                    leftSpacer,
                    Priority.SOMETIMES
            );
    
            final Pane rightSpacer = new Pane();
            HBox.setHgrow(
                    rightSpacer,
                    Priority.SOMETIMES
            );
    
            final ToolBar toolBar = new ToolBar(
                    new Button("Good"),
                    new Button("Boys"),
                    leftSpacer,
                    new Button("Deserve"),
                    new Button("Fruit"),
                    rightSpacer,
                    new Button("Always")
            );
            toolBar.setPrefWidth(400);
    
            BorderPane layout = new BorderPane();
            layout.setTop(toolBar);
    
            stage.setScene(
                    new Scene(
                            layout
                    )
            );
            stage.show();
        }
        public static void main(String[] args) { launch(); }
    }  
    

    A ToolBar (at least in the standard modena.css stylesheet for Java 8), is already internally implemented as a HBox container, so you can just use the HBox.setHgrow method to adjust the internal layout constraints of items you place in a toolbar.

    The left and right spacers in this example are implemented as blank panes which will just grow and shrink to fit available space.

    If you wanted a spacer which was a fixed size, instead of HBox.setHgrow, you could use something like:

    leftSpacer.setPrefSize(20); 
    leftSpacer.setMinSize(HBox.USE_PREF_SIZE); 
    leftSpacer.setMaxSize(HBox.USE_PREF_SIZE);
    

提交回复
热议问题