JavaFX CheckBox Create

Introduction

To Create CheckBox in JavaFX

// Create the check boxes.
cbWeb = new CheckBox("Web");
cbDesktop = new CheckBox("Dekstop");
cbMobile = new CheckBox("Mobile");
    

Full source

// Demonstrate Check boxes. 

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {

  private CheckBox cbWeb, cbDesktop, cbMobile;

  private Label response;

  public static void main(String[] args) {
    launch(args);/*from   w w w.  j a va  2  s .c o  m*/
  }

  public void start(Stage myStage) {
    myStage.setTitle("Demonstrate Checkboxes");

    FlowPane rootNode = new FlowPane(10, 10);

    rootNode.setAlignment(Pos.CENTER);

    Scene myScene = new Scene(rootNode, 230, 140);

    myStage.setScene(myScene);

    Label heading = new Label("Select Deployment Options");

    // Create a label that will report the state of the
    // selected check box.
    response = new Label("No Deployment Selected");

    // Create the check boxes.
    cbWeb = new CheckBox("Web");
    cbDesktop = new CheckBox("Dekstop");
    cbMobile = new CheckBox("Mobile");

    // Handle action events for the check boxes.
    cbWeb.setOnAction(e->{
        if (cbWeb.isSelected())
          response.setText("Web selected.");
        else
          response.setText("Web cleared.");
    });

    cbDesktop.setOnAction(e->{
        if (cbDesktop.isSelected())
          response.setText("Desktop selected.");
        else
          response.setText("Desktop cleared.");
    });

    cbMobile.setOnAction(e->{
        if (cbMobile.isSelected())
          response.setText("Mobile selected.");
        else
          response.setText("Mobile cleared.");
    });

    // Use a separator to better organize the layout.
    Separator separator = new Separator();
    separator.setPrefWidth(200);

    rootNode.getChildren().addAll(heading, separator, cbWeb, cbDesktop, cbMobile, response);

    myStage.show();
  }
}



PreviousNext

Related