In this JavaFX tutorial we are going to learn How to Create RadioButton in JavaFX, so a
radio button or option button is a graphical control element that allows the user to choose
only one of a predefined set of mutually exclusive options.
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 source code for How to Create RadioButton 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.io.FileInputStream; import java.io.FileNotFoundException; public class MyRadioButton extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws FileNotFoundException { stage.setTitle("JavaFX Radio Button - Codeloop.org"); HBox root = new HBox(); root.setPadding(new Insets(10)); root.setSpacing(10); ToggleGroup group = new ToggleGroup(); RadioButton football = new RadioButton("Football"); football.setToggleGroup(group); football.setSelected(true); FileInputStream in1 = new FileInputStream("images/football.png"); Image im1 = new Image(in1); ImageView imageView = new ImageView(im1); football.setGraphic(imageView); RadioButton cricket = new RadioButton("Cricket"); cricket.setToggleGroup(group); FileInputStream in2 = new FileInputStream("images/cricket.png"); Image im2 = new Image(in2); ImageView imageView2 = new ImageView(im2); cricket.setGraphic(imageView2); RadioButton tennis = new RadioButton("Tennis"); tennis.setToggleGroup(group); FileInputStream in3 = new FileInputStream("images/tennis.png"); Image im3 = new Image(in3); ImageView imageView3 = new ImageView(im3); tennis.setGraphic(imageView3); root.getChildren().addAll(football, cricket, tennis); Scene scene =new Scene(root, 400,400); stage.setScene(scene); stage.show(); } } |
You can create javafx radiobutton by creating the object of RadioButton class, and you need to
give the text for the radio button.
1 |
RadioButton cricket = new RadioButton("Cricket"); |
This is our HBox layout. we want to add our widgets in this hbox container. also we have used
some padding with spacing for our hbox layout.
1 2 3 |
HBox root = new HBox(); root.setPadding(new Insets(10)); root.setSpacing(10); |
We need to add our radio buttons in a toggle group.
1 |
ToggleGroup group = new ToggleGroup(); |
If you want your RadioButton should be selected by default, you can use this code.
1 |
football.setSelected(true); |
Also we are going to add graphics or icons to our radio buttons, so first of all make sure that
you have added some icons in your working directory. and this code is for loading the icon or
image.
1 2 3 |
FileInputStream in1 = new FileInputStream("images/football.png"); Image im1 = new Image(in1); ImageView imageView = new ImageView(im1); |
And in here we set the icon or graphic to the javafx radiobutton.
1 |
football.setGraphic(imageView); |
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 radiobutton,
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(football, cricket, tennis); |
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 400 width and 300 height for the
window.
1 |
Scene scene =new Scene(root, 400,400); |
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 will be the result.
Subscribe and Get Free Video Courses & Articles in your Email