Saturday 2 June 2018

JavaFX: CheckBox: States of checkbox

CheckBox can be either in defined (Selected/notSelected) (or) undefined states. You can able to select/unselect the checkbox, when the checkbox is in defined state. You can't perform select/unselect the checkbox when the checkbox is in undefined state.

How can I put the checkbox in defined/undefined states?
By using setSelected (), setIndeterminate () methods, you can put the checkbox in defined/undefined states.

setSelected 
setIndeterminate
StateOfCheckBox

true
false
defined
false
false
defined
true
true
undefined
false
true
undefined

Find the below working application.

CheckBoxApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

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

public class CheckBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {

  CheckBox definedCheckBox1 = new CheckBox("definedCheckBox1");
  definedCheckBox1.setSelected(true);
  definedCheckBox1.setIndeterminate(false);

  CheckBox definedCheckBox2 = new CheckBox("definedCheckBox2");
  definedCheckBox2.setSelected(false);
  definedCheckBox2.setIndeterminate(false);

  CheckBox unDefinedCheckBox1 = new CheckBox("unDefinedCheckBox1");
  unDefinedCheckBox1.setSelected(true);
  unDefinedCheckBox1.setIndeterminate(true);

  CheckBox unDefinedCheckBox2 = new CheckBox("unDefinedCheckBox2");
  unDefinedCheckBox2.setSelected(false);
  unDefinedCheckBox2.setIndeterminate(true);

  VBox vbox = new VBox(10, definedCheckBox1, definedCheckBox2, unDefinedCheckBox1, unDefinedCheckBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX CheckBox Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}


TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(CheckBoxApp.class, args);
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment