How to create a ButtonBar in JavaFX?



A ButtonBar is simply an HBox on which you can arrange buttons. Typically, the buttons on a ButtonBar are Operating System specific. You can create a button bar by instantiating the javafx.scene.control.ButtonBar class.

Example

The following Example demonstrates the creation of a ButtonBar.

import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonBar.ButtonData; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class ButtonBarExample extends Application {    @Override    public void start(Stage stage) {       //Creating toggle buttons       ToggleButton button1 = new ToggleButton("Java");       button1.setPrefSize(60, 40);       ToggleButton button2 = new ToggleButton("Python");       button2.setPrefSize(60, 40);       ToggleButton button3 = new ToggleButton("C++");       button3.setPrefSize(60, 40);       //Adding the buttons to a toggle group       ToggleGroup group = new ToggleGroup();       button1.setToggleGroup(group);       button2.setToggleGroup(group);       button3.setToggleGroup(group);       //Create a ButtonBar       ButtonBar buttonBar = new ButtonBar();       //Adding the buttons to the button bar       ButtonBar.setButtonData(button1, ButtonData.APPLY);       ButtonBar.setButtonData(button2, ButtonData.APPLY);       ButtonBar.setButtonData(button3, ButtonData.APPLY);       buttonBar.getButtons().addAll(button1, button2, button3);       //Adding the toggle button to the pane       HBox box = new HBox(5);       box.setPadding(new Insets(50, 50, 50, 150));       box.getChildren().addAll(buttonBar);       box.setStyle("-fx-background-color: BEIGE");       //Setting the stage       Scene scene = new Scene(box, 595, 150);       stage.setTitle("Button Bar");       stage.setScene(scene);       stage.show();    }    public static void main(String args[]){       launch(args);    } }

Output

Updated on: 2020-05-20T07:39:30+05:30

878 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements