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 Class
import
javafx.application.Application;
import
javafx.geometry.Insets;
import
javafx.scene.Scene;
import
javafx.scene.control.Label;
import
javafx.scene.control.ToggleButton;
import
javafx.scene.control.ToggleGroup;
import
javafx.scene.layout.HBox;
import
javafx.stage.Stage;
public
class
ToggleButtonExample
extends
Application {
public
void
start(Stage stage)
{
// Hbox layout
HBox root =
new
HBox();
root.setPadding(
new
Insets(
10
));
root.setSpacing(
5
);
// Gender
root.getChildren().add(
new
Label(
"Your gender:"
));
// Creating a ToggleGroup
ToggleGroup group =
new
ToggleGroup();
// Creating new Toggle buttons.
ToggleButton maleButton =
new
ToggleButton(
"Male"
);
ToggleButton femaleButton =
new
ToggleButton(
"Female"
);
// Set toggle group
// In a group, maximum only
// one button is selected
maleButton.setToggleGroup(group);
femaleButton.setToggleGroup(group);
maleButton.setUserData(
"I am a Male"
);
femaleButton.setUserData(
"I am a Female"
);
// male button is selected at first by default
maleButton.setSelected(
true
);
root.getChildren().addAll(maleButton, femaleButton);
// create the scene
Scene scene =
new
Scene(root,
450
,
300
);
stage.setTitle(
"Toggle Button"
);
stage.setScene(scene);
stage.show();
}
// Main Method
public
static
void
main(String[] args)
{
launch(args);
}
}
Output:
- 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 ChangeListener
import
javafx.application.Application;
import
javafx.beans.value.ChangeListener;
import
javafx.beans.value.ObservableValue;
import
javafx.scene.Scene;
import
javafx.scene.control.Label;
import
javafx.scene.control.Toggle;
import
javafx.scene.control.ToggleButton;
import
javafx.scene.control.ToggleGroup;
import
javafx.scene.layout.HBox;
import
javafx.scene.layout.VBox;
import
javafx.stage.Stage;
public
class
ToggleButtonDemo
extends
Application {
// Create the Message Label
Label selectionMsg =
new
Label(
"Your selection: None"
);
public
void
start(Stage stage)
{
// Create four ToggleButtons
ToggleButton csBtn =
new
ToggleButton(
"Computer Science"
);
ToggleButton pBtn =
new
ToggleButton(
"Physics"
);
ToggleButton chemBtn =
new
ToggleButton(
"Chemistry"
);
ToggleButton mBtn =
new
ToggleButton(
"Maths"
);
// Create a ToggleGroup
final
ToggleGroup group =
new
ToggleGroup();
// Add all ToggleButtons to a ToggleGroup
group.getToggles().addAll(csBtn, pBtn, chemBtn, mBtn);
// Create a ChangeListener for the ToggleGroup
group.selectedToggleProperty().addListener(
new
ChangeListener<Toggle>()
{
public
void
changed(ObservableValue<?
extends
Toggle> ov,
final
Toggle toggle,
final
Toggle new_toggle)
{
String toggleBtn = ((ToggleButton)new_toggle).getText();
selectionMsg.setText(
"Your selection: "
+ toggleBtn);
}
});
// Create the Label for the Selection
Label selectLbl =
new
Label(
"Select the subject :"
);
// Create a HBox
HBox buttonBox =
new
HBox();
// Add ToggleButtons to an HBox
buttonBox.getChildren().addAll(csBtn, pBtn, chemBtn, mBtn);
// Set the spacing between children to 10px
buttonBox.setSpacing(
10
);
// Create the VBox
VBox root =
new
VBox();
// Add the Labels and HBox to the VBox
root.getChildren().addAll(selectionMsg, selectLbl, buttonBox);
// Set the spacing between children to 10px
root.setSpacing(
10
);
// Set the Size of the VBox
root.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 VBox
root.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 Scene
Scene scene =
new
Scene(root);
// Add the scene to the Stage
stage.setScene(scene);
// Set the title of the Stage
stage.setTitle(
"A ToggleButton Example"
);
// Display the Stage
stage.show();
}
// Main Method
public
static
void
main(String[] args)
{
// launch the application
Application.launch(args);
}
}
Output:
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