Example usage for javafx.scene.control ToggleButton ToggleButton

List of usage examples for javafx.scene.control ToggleButton ToggleButton

Introduction

In this page you can find the example usage for javafx.scene.control ToggleButton ToggleButton.

Prototype

public ToggleButton(String text) 

Source Link

Document

Creates a toggle button with the specified text as its label.

Usage

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Toggle Button Sample");
    stage.setWidth(250);/*ww  w . ja  v  a2  s  . com*/
    stage.setHeight(180);

    final ToggleGroup group = new ToggleGroup();

    group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
        public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
            System.out.println((Color) group.getSelectedToggle().getUserData());
        }
    });

    ToggleButton tb1 = new ToggleButton("A");
    tb1.setToggleGroup(group);
    tb1.setUserData(Color.LIGHTGREEN);
    tb1.setSelected(true);

    ToggleButton tb2 = new ToggleButton("B");
    tb2.setToggleGroup(group);
    tb2.setUserData(Color.LIGHTBLUE);
    ToggleButton tb3 = new ToggleButton("C");
    tb3.setToggleGroup(group);
    tb3.setUserData(Color.SALMON);

    HBox hbox = new HBox();

    hbox.getChildren().add(tb1);
    hbox.getChildren().add(tb2);
    hbox.getChildren().add(tb3);

    ((Group) scene.getRoot()).getChildren().add(hbox);
    stage.setScene(scene);
    stage.show();
}