In this JavaFX tutorial we are going to learn How to Create ToggleButton in JavaFX, so a
ToggleButton is a specialized control which has the ability to be selected. Typically a
ToggleButton is rendered similarly to a Button. however, they are two different types of controls.
a Button is a “command” button which invokes a function when clicked. A ToggleButton on
the other hand is simply a control with a Boolean indicating whether it has been selected or not.
Python GUI Development Tutorials with Video Training
- PyQt5 GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development Tutorials
- Kivy GUI Development Tutorials
- wxPython GUI Development Tutorials
This is the complete code for How to Create ToggleButton in JavaFX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class MyToggleButton extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("JavaFX Toggle Button - Codeloop.org"); HBox root = new HBox(); root.setPadding(new Insets(10)); root.setSpacing(5); ToggleGroup group = new ToggleGroup(); ToggleButton men = new ToggleButton("Men"); ToggleButton women = new ToggleButton("Women"); men.setToggleGroup(group); women.setToggleGroup(group); men.setUserData("Iam A Man"); women.setUserData("Iam A Women"); men.setSelected(true); root.getChildren().addAll(men, women); Scene scene =new Scene(root, 300,300); stage.setScene(scene); stage.show(); } } |
You can create javafx togglebutton by creating the object of ToggleButton class, and you need to
give the text for the radio button.
1 |
ToggleButton men = new ToggleButton("Men"); |
Because we want to add our togglebutton in a ToggleGroup, so you need to create the object
of togglegroup.
1 |
ToggleGroup group = new ToggleGroup(); |
So in here we have added our two togglebutton in the togglegroup.
1 2 |
men.setToggleGroup(group); women.setToggleGroup(group); |
If you want the togglebutton be selected you can use this code.
1 |
men.setSelected(true); |
When you create a widget in JavaFX, for example button,label,combobox or some other
widgets, you need to add that to your container, in here we need to add our two togglebutton
in HBox container, because we want to align the widgets horizontally.
if you want to align widgets vertically you can use VBox layout.
1 |
root.getChildren().addAll(men, women); |
Also for every JavaFX application we need to create a Scene object. in the scene we need
to add our container with the width and height of the window, if you want to colorize your
window, you can do it in here. you can see that we have given 300 width and 300 height for the
window.
1 |
Scene scene =new Scene(root, 300,300); |
At the end you need to set your scene object to the stage of the window, and show the window.
1 2 |
stage.setScene(scene); stage.show(); |
Run the complete code and this is the result.
Subscribe and Get Free Video Courses & Articles in your Email