ButtonBar
ButtonBar is a special JavaFX container designed to arrange buttons in a consistent way, especially for dialogs like OK / Cancel / Apply.
It automatically organizes buttons according to the operating system style.
Example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ButtonBarExample extends Application {
@Override
public void start(Stage stage) {
Button saveButton = new Button("Save");
Button cancelButton = new Button("Cancel");
Button helpButton = new Button("Help");
ButtonBar buttonBar = new ButtonBar();
buttonBar.getButtons().addAll(saveButton, cancelButton, helpButton);
VBox root = new VBox(20, buttonBar);
Scene scene = new Scene(root, 300, 120);
stage.setTitle("ButtonBar Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Without ButtonBar, if you use an HBox, the buttons stay exactly in the order you added them.
With ButtonBar, JavaFX may rearrange them depending on the platform. For example:
- On Windows:
Help Save Cancel - On macOS:
Cancel Save
This helps your app feel more native.
You can control the role of each button:
ButtonBar.setButtonData(saveButton, ButtonBar.ButtonData.OK_DONE);
ButtonBar.setButtonData(cancelButton, ButtonBar.ButtonData.CANCEL_CLOSE);
ButtonBar.setButtonData(helpButton, ButtonBar.ButtonData.HELP);
Full example:
Button saveButton = new Button("Save");
Button cancelButton = new Button("Cancel");
Button helpButton = new Button("Help");
ButtonBar.setButtonData(saveButton, ButtonBar.ButtonData.OK_DONE);
ButtonBar.setButtonData(cancelButton, ButtonBar.ButtonData.CANCEL_CLOSE);
ButtonBar.setButtonData(helpButton, ButtonBar.ButtonData.HELP);
ButtonBar buttonBar = new ButtonBar();
buttonBar.getButtons().addAll(helpButton, saveButton, cancelButton);
Even if you add them in a different order, ButtonBar may place them correctly for the OS.
Common ButtonData values:
ButtonBar.ButtonData.OK_DONE
ButtonBar.ButtonData.CANCEL_CLOSE
ButtonBar.ButtonData.YES
ButtonBar.ButtonData.NO
ButtonBar.ButtonData.FINISH
ButtonBar.ButtonData.HELP
ButtonBar.ButtonData.APPLY
FXML example:
<ButtonBar>
<buttons>
<Button text="Save"
ButtonBar.buttonData="OK_DONE"/>
<Button text="Cancel"
ButtonBar.buttonData="CANCEL_CLOSE"/>
</buttons>
</ButtonBar>
Typical use cases:
- dialog windows
- settings pages
- confirmation forms
- save/cancel areas at the bottom of a page
For example, in your JavaFX settings screens or monitor configuration pages, a ButtonBar at the bottom is often cleaner than a normal HBox:
BorderPane root = new BorderPane();
ButtonBar actions = new ButtonBar();
actions.getButtons().addAll(saveButton, cancelButton);
root.setBottom(actions);
Difference between HBox and ButtonBar:
| HBox | ButtonBar |
|---|---|
| Just puts buttons in a row | Organizes buttons according to OS conventions |
| Manual spacing/order | Automatic dialog-style order |
| General-purpose layout | Specifically made for buttons |