JavaFX Checkbox


Javafx Checkbox tutorial:

JavaFX Checkbox UI control in which user can select using checkmark with one or more options.

Constructors of checkbox:

  • CheckBox()
    Creates a check box with an empty string for its label.
  • CheckBox(String text)
    Creates a check box with the specified text as its label.

Example using check box :

package javafxtuts;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

 * @author javafxtuts
public class Javafxtuts extends Application {


  public void start(Stage primaryStage) {
      // setting title of application
      //Vbox layout
      VBox root = new VBox();
      Label lb =new Label("Select language You Know :");
      //to set space for label from other nodes of VBox
      lb.setPadding(new Insets(10));
    //creating new checkboxs. 
      CheckBox cb1 = new CheckBox("  C  ");
      CheckBox cb2 = new CheckBox("  Java  ");
       CheckBox cb3 = new CheckBox("  JavaFX  ");
      //pre select a check Box
      // to set space for root
      root.setPadding(new Insets(10));
      //add all children of Vbox layout(root)
      Scene scene = new Scene(root, 400, 300);


     * @param args the command line arguments
    public static void main(String[] args) {

Output :

Explanation of check box example :

    • Create a VBox Layout

VBox root=new VBox();

    • Create a Label AS

Label lb =new Label();

    • Set space or padding using setPadding() method


    • Which set the space of 10 for cb2 in all directions.
    • To Pre selected a checkbox we use a method setSelected()


  • It mark the cb2 checkbox when application execute.
  • And finally add all children of root to the root using addAll() method.

Leave A Reply