In this Java GUI article we are going to learn about Creating CheckBox in JavaFX,
so a checkbox is a GUI widget that permits the user to make a binary choice, i.e. a choice
between one of two possible mutually exclusive options. For example, the user may have to
answer ‘yes’ or ‘no’ on a simple yes/no question.
Also you can check Python GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
5: wxPython GUI Development Tutorials
You can create javafx checkbox by creating the object of CheckBox class and you need to
give the text for the checkbox.
1 |
CheckBox check1 = new CheckBox("Football"); |
So now this is the complete code for Java GUI – Creating CheckBox 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 |
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.CheckBox; 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 MyCheckBox extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws FileNotFoundException { stage.setTitle("JavaFx CheckBox"); HBox hbox = new HBox(); hbox.setPadding(new Insets(5)); hbox.setSpacing(10); CheckBox check1 = new CheckBox("Football"); FileInputStream in1 = new FileInputStream("images/football.png"); Image im1 = new Image(in1); ImageView imageView = new ImageView(im1); check1.setGraphic(imageView); CheckBox check2 = new CheckBox("Cricket"); FileInputStream in2 = new FileInputStream("images/cricket.png"); Image im2 = new Image(in2); ImageView imageView1 = new ImageView(im2); check2.setGraphic(imageView1); CheckBox check3 = new CheckBox("Tennis"); FileInputStream in3 = new FileInputStream("images/tennis.png"); Image im3 = new Image(in3); ImageView imageView2 = new ImageView(im3); check3.setGraphic(imageView2); hbox.getChildren().addAll(check1, check2, check3); Scene scene = new Scene(hbox, 300, 200); stage.setScene(scene); stage.show(); } } |
In every JavaFX application we need to create a container, a container is like layout. as we
have created an HBox container in the above code, also we are going to create padding with
spacing for the HBox Layout.
1 2 3 |
HBox hbox = new HBox(); hbox.setPadding(new Insets(5)); hbox.setSpacing(10); |
Using this code we create javafx checkbox, also in here iam going to add icons for every
checkbox. make sure that you have already added an icon to your working directory.
1 2 3 4 5 |
CheckBox check1 = new CheckBox("Football"); FileInputStream in1 = new FileInputStream("images/football.png"); Image im1 = new Image(in1); ImageView imageView = new ImageView(im1); check1.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 three CheckBox
in to HBox container.
1 |
hbox.getChildren().addAll(check1, check2, check3); |
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 200 height for the
window.
1 |
Scene scene = new Scene(hbox, 300, 200); |
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