Home > OS >  How to remove extra space after separator in toolbar in JavaFX?
How to remove extra space after separator in toolbar in JavaFX?

Time:05-27

This is my code:

public class NewMain1 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        HBox root = new HBox();
        ToolBar toolbar = new ToolBar();
        toolbar.getItems().addAll(new TextField(), new Separator(), new Button("foo"), new Button("bar"));

        root.getChildren().addAll(toolbar);
        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }
}

And this is the result:

enter image description here

As you can see the distance between a text field and a separator (left margin) is less then the distance between the separator and a button (right margin).

Could anyone say how to decrease right margin that right margin to be equal to the left margin using CSS?

CodePudding user response:

Have you tryed using scene builder with fxml? That way you can drag the elements and set the margins between them.

CodePudding user response:

I checked in enter image description here

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ToolBarApp extends Application {
    @Override
    public void start(Stage primaryStage) {
        HBox root = new HBox();
        ToolBar toolbar = new ToolBar();

        Separator sep = new Separator();
        toolbar.getItems().addAll(
                new TextField(),
                sep,
                new Button("foo"),
                new Button("bar")
        );
        HBox.setMargin(sep, new Insets(0, -2.5, 0, 0));

        root.getChildren().addAll(
                toolbar
        );

        Scene scene = new Scene(root, 400, 400);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  • Related