JavaFX | ToggleButton Class
Last Updated :
30 Aug, 2018
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
// 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
// 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
Similar Reads
Java Swing | JToggleButton Class A JToggleButton is a two-state button. The two states are selected and unselected. The JRadioButton and JCheckBox classes are subclasses of this class. When the user presses the toggle button, it toggles between being pressed or unpressed. JToggleButton is used to select a choice from a list of poss
4 min read
JavaFX | ToolBar Class ToolBar class is a part of JavaFX. A ToolBar is a control which displays items vertically or horizontally. Buttons, ToggleButtons, and Separators are generally placed into ToolBar. You can also insert any Node into them. Overflow button will appear if there are too many items to fit in the ToolBar w
4 min read
JavaFX | Tab Class Tab class is a part of JavaFX. Tab class creates an object that is contained in TabPane. A Tab can contain any node as its content. A TabPane can contain multiple tabs. When the user clicks on the tab the content of the tab is made visible to the user. Constructors of the class: Tab(): Creates a new
5 min read
JavaFX | TabPane Class TabPane class is a part of JavaFX. TabPane allows switching between several tabs. TabPane acts as a container of tabs. The positions of the tabs can be specified using the setSide() function. when the tabs do not fit in the TabPane, a menu button appears at the upper right corner of TabPane which di
5 min read
JavaFX | TitledPane Class TitledPane class is a part of JavaFX. TitledPane class creates a panel with a title which can be opened or closed. TitledPane class extends the Labeled class. Constructor of the class: TitledPane(): Creates a new TitledPane object. TitledPane(String t, Node n): Creates a new TitledPane object with s
4 min read
JavaFX | Popup Class Popup class is a part of JavaFX. Popup class creates a popup with no content, a null fill and is transparent. Popup class is used to display a notification, buttons, or a drop-down menu and so forth. The popup has no decorations. It essentially acts as a specialized scene/window which has no decorat
4 min read
JavaFX | Slider Class A Slider is a Control in JavaFX which is used to display a continuous or discrete range of valid numeric choices and allows the user to interact with the control. A slider is rendered as a vertical or horizontal bar with a knob that the user can slide to indicate the desired value. A slider can also
5 min read
JavaFX | MenuButton MenuButton is a part of the JavaFX library. The menuButton when pressed shows a context menu that displays a set of items and the user may select any item. It typically contains several menu items and the user may select at most one MenuItem at a time. Constructor of the MenuButton class are: MenuB
4 min read
ToggleButton in Android ToggleButton is used to allow users to perform two operations on a single button. These are used to perform on and off operations like that of Switch. ToggleButton can perform two different operations on clicking on it. In this article, we will take a look at How to implement ToggleButton in Android
4 min read
JavaFX | RadioButton with examples RadioButtons are a part of JavaFx package. RadioButtons are mainly used to create a series of items where only one can be selected. When a Radio button is pressed and released an Action event is sent, this Action Event can be handled using an Event Handler. RadioButton can be added to Toggle Group s
6 min read