JavaFX Tutorial - Java CheckBox(java.lang.String text) Constructor








Syntax

CheckBox(java.lang.String text) constructor from CheckBox has the following syntax.

public CheckBox(java.lang.String text)

Example

In the following code shows how to use CheckBox.CheckBox(java.lang.String text) constructor.

/*w w  w.  j  a va 2s  .c  o m*/
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Tooltip Sample");
        stage.setWidth(300);
        stage.setHeight(150);
        final CheckBox cb = new CheckBox("checkBox");
        final Tooltip tooltip = new Tooltip("$ tooltip");
        tooltip.setFont(new Font("Arial", 16));
        cb.setTooltip(tooltip);
        cb.selectedProperty().addListener(new ChangeListener<Boolean>() {
           public void changed(ObservableValue<? extends Boolean> ov,
             Boolean old_val, Boolean new_val) {
             System.out.println(cb.isSelected());
          }
        });

        ((Group) scene.getRoot()).getChildren().add(cb);

        stage.setScene(scene);
        stage.show();
    }
}