Friday 1 June 2018

JavaFX: Create check box

CheckBox class provides below constructors to create check box instances.

public CheckBox()
public CheckBox(String text)

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

CheckBox checkBox2 = new CheckBox("Cricket");

Find the below working example.

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");

  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