How to make ComboBox grow to max width in ToolBar in JavaFX?

  Kiến thức lập trình

I need to make ComboBox to grow to max width in ToolBar. This is my code:

public class JavaFxTest7 extends Application {

    @Override
    public void start(Stage stage) {
        var comboBox = new ComboBox<String>();
        comboBox.setEditable(true);
        comboBox.setMaxWidth(Double.MAX_VALUE);
        var button = new Button("OK");
        var toolbar = new ToolBar(comboBox, button);

        Scene scene = new Scene(new VBox(toolbar), 400, 200);
        stage.setScene(scene);
        stage.show();
    }

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

And this is the result:

enter image description here

As you see, it didn’t work, as ComboBox didn’t grow, as there is free space after button. Could anyone say how to do it?

LEAVE A COMMENT