Friday, 1 June 2018

JavaFX: Select the checkbox by default

By using 'setSelected' method of CheckBox class, you can select the check box by default.

Ex
CheckBox checkBox1 = new CheckBox();
checkBox1.setText("Chess");
checkBox1.setSelected(true);

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 checkBox1 = new CheckBox();
  checkBox1.setText("Chess");
  checkBox1.setSelected(true);

  CheckBox checkBox2 = new CheckBox("Cricket");

  VBox vbox = new VBox(10, checkBox1, checkBox2);

  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