JavaFX | ToggleButton Class
A ToggleButton is a special control having the ability to be selected. Basically, ToggleButton is rendered similarly to a Button but these two are the different types of Controls. A Button is a “command” button that invokes a function when clicked. But a ToggleButton is a control with a Boolean indicating whether it is selected. It inherits the ButtonBase class.
ToggleButton can also be placed in groups. By default, a ToggleButton is not in a group. When in groups, only one ToggleButton at a time within that group can be selected. To put two ToggleButtons in the same group, simply assign them both the same value for ToggleGroup. Unlike RadioButtons, ToggleButtons in a ToggleGroup does not attempt to force at least one selected ToggleButton in the group. Means, if a ToggleButton is selected, clicking on it will cause it to become unselected. With RadioButton, clicking on the selected button in the group will have no effect.
Constructors of the class:
- ToggleButton(): Creates a toggle button with an empty string for its label.
- ToggleButton(String txt): Creates a toggle button with the specified text as its label.
- ToggleButton(String txt, Node graphic): Creates a toggle button with the specified text and icon for its label.
Commonly Used Methods:
| Method | Description |
|---|---|
| setToggleGroup(ToggleGroup val) | Sets the value of the property toggleGroup. |
| setSelected(boolean val) | Sets the value of the property selected. |
| isSelected() | Gets the value of the property selected. |
| fire() | Invoked when a user gesture indicates that an event for this ButtonBase should occur. |
Below programs illustrate the use of ToggleButton class:
- Simple Java program to demonstrate ToggleButton Class: In this program we are trying to select the gender of a person. We first Create HBox and then set the Layout for it. Create a ToggleGroup and new Toggle Buttons named”Male” and “Female“. Set the Toggle Group (in a group only one button can be selected at a time) using setToggleGroup() method. By default Male button is selected. Create the scene and set scene to the stage using setScene() method. And launch the application.
// Java program to demonstrate ToggleButton Classimportjavafx.application.Application;importjavafx.geometry.Insets;importjavafx.scene.Scene;importjavafx.scene.control.Label;importjavafx.scene.control.ToggleButton;importjavafx.scene.control.ToggleGroup;importjavafx.scene.layout.HBox;importjavafx.stage.Stage;publicclassToggleButtonExampleextendsApplication {publicvoidstart(Stage stage){// Hbox layoutHBox root =newHBox();root.setPadding(newInsets(10));root.setSpacing(5);// Genderroot.getChildren().add(newLabel("Your gender:"));// Creating a ToggleGroupToggleGroup group =newToggleGroup();// Creating new Toggle buttons.ToggleButton maleButton =newToggleButton("Male");ToggleButton femaleButton =newToggleButton("Female");// Set toggle group// In a group, maximum only// one button is selectedmaleButton.setToggleGroup(group);femaleButton.setToggleGroup(group);maleButton.setUserData("I am a Male");femaleButton.setUserData("I am a Female");// male button is selected at first by defaultmaleButton.setSelected(true);root.getChildren().addAll(maleButton, femaleButton);// create the sceneScene scene =newScene(root,450,300);stage.setTitle("Toggle Button");stage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String[] args){launch(args);}}chevron_rightfilter_noneOutput:
- Java program to demonstrate ToggleButton Class using ChangeListener: In this program, we first create a label. Then we will create Toggle Buttons using ToggleButton() and Toggle Group is created using ToggleGroup() method. Add all the Toggle Buttons to the ToggleGroup. Now create a ChangeListener for the ToggleGroup. A ChangeListener is notified whenever the value of an ObservableValue change. An ObservableValue is an entity that wraps a value and allows to observe the value for changes. Now, create the label for the selection of the subjects. Create a HBox using HBox() Now, add ToggleButtons to an HBox. Set the spacing between the buttons using setSpacing() method. Create the VBox, add labels and HBox to the VBox. Set the size of the VBox and set padding of the VBox (e.g border-style, border-width, border-radius, border-insets, border-color). Create the scene and add it to the stage. Set the title of the stage and display.
// Java program to demonstrate ToggleButton// Class using ChangeListenerimportjavafx.application.Application;importjavafx.beans.value.ChangeListener;importjavafx.beans.value.ObservableValue;importjavafx.scene.Scene;importjavafx.scene.control.Label;importjavafx.scene.control.Toggle;importjavafx.scene.control.ToggleButton;importjavafx.scene.control.ToggleGroup;importjavafx.scene.layout.HBox;importjavafx.scene.layout.VBox;importjavafx.stage.Stage;publicclassToggleButtonDemoextendsApplication {// Create the Message LabelLabel selectionMsg =newLabel("Your selection: None");publicvoidstart(Stage stage){// Create four ToggleButtonsToggleButton csBtn =newToggleButton("Computer Science");ToggleButton pBtn =newToggleButton("Physics");ToggleButton chemBtn =newToggleButton("Chemistry");ToggleButton mBtn =newToggleButton("Maths");// Create a ToggleGroupfinalToggleGroup group =newToggleGroup();// Add all ToggleButtons to a ToggleGroupgroup.getToggles().addAll(csBtn, pBtn, chemBtn, mBtn);// Create a ChangeListener for the ToggleGroupgroup.selectedToggleProperty().addListener(newChangeListener<Toggle>(){publicvoidchanged(ObservableValue<?extendsToggle> ov,finalToggle toggle,finalToggle new_toggle){String toggleBtn = ((ToggleButton)new_toggle).getText();selectionMsg.setText("Your selection: "+ toggleBtn);}});// Create the Label for the SelectionLabel selectLbl =newLabel("Select the subject :");// Create a HBoxHBox buttonBox =newHBox();// Add ToggleButtons to an HBoxbuttonBox.getChildren().addAll(csBtn, pBtn, chemBtn, mBtn);// Set the spacing between children to 10pxbuttonBox.setSpacing(10);// Create the VBoxVBox root =newVBox();// Add the Labels and HBox to the VBoxroot.getChildren().addAll(selectionMsg, selectLbl, buttonBox);// Set the spacing between children to 10pxroot.setSpacing(10);// Set the Size of the VBoxroot.setMinSize(350,250);// Set the padding of the VBox// Set the border-style of the VBox// Set the border-width of the VBox// Set the border-insets of the VBox// Set the border-radius of the VBox// Set the border-color of the VBoxroot.setStyle("-fx-padding: 10;"+"-fx-border-style: solid inside;"+"-fx-border-width: 2;"+"-fx-border-insets: 5;"+"-fx-border-radius: 5;"+"-fx-border-color: blue;");// Create the SceneScene scene =newScene(root);// Add the scene to the Stagestage.setScene(scene);// Set the title of the Stagestage.setTitle("A ToggleButton Example");// Display the Stagestage.show();}// Main Methodpublicstaticvoidmain(String[] args){// launch the applicationApplication.launch(args);}}chevron_rightfilter_noneOutput:
- Java.util.BitSet class methods in Java with Examples | Set 2
- More restrictive access to a derived class method in Java
- java.lang.Character class methods | Set 1
- Static class in Java
- Inner class in java
- BigInteger Class in Java
- Scanner Class in Java
- Java.Lang.Float class in Java
- Does JVM create object of Main class (the class with main())?
- Difference between Scanner and BufferReader Class in Java
- Myth about the file name and class name in Java
- Arrays class in Java
- Date class in Java (With Examples)
- How to create Immutable class in Java?
- Java Object Creation of Inherited Class
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ToggleButton.html
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



