Lecture Videos
  1  import javafx.event.ActionEvent;
  2  import javafx.event.EventHandler;
  3  import javafx.geometry.Insets;
  4  import javafx.scene.Scene;
  5  import javafx.scene.control.CheckBox;
  6  import javafx.scene.layout.BorderPane;
  7  import javafx.scene.layout.VBox;
  8  import javafx.scene.text.Font;
  9  import javafx.scene.text.FontPosture;
 10  import javafx.scene.text.FontWeight;
 11  import javafx.stage.Stage;
 12  
 13  public class CheckBoxDemo extends ButtonDemo {
 14    @Override // Override the getPane() method in the super class
 15    protected BorderPane getPane() {
 16      BorderPane pane = super.getPane();
 17  
 18      Font fontBoldItalic = Font.font("Times New Roman", 
 19        FontWeight.BOLD, FontPosture.ITALIC, 20);
 20      Font fontBold = Font.font("Times New Roman", 
 21        FontWeight.BOLD, FontPosture.REGULAR, 20);
 22      Font fontItalic = Font.font("Times New Roman", 
 23        FontWeight.NORMAL, FontPosture.ITALIC, 20);
 24      Font fontNormal = Font.font("Times New Roman", 
 25        FontWeight.NORMAL, FontPosture.REGULAR, 20);
 26      
 27      text.setFont(fontNormal);
 28      
 29      VBox paneForCheckBoxes = new VBox(20);
 30      paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5)); 
 31      paneForCheckBoxes.setStyle("-fx-border-color: green");
 32      CheckBox chkBold = new CheckBox("Bold");
 33      CheckBox chkItalic = new CheckBox("Italic");
 34      paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic);
 35      pane.setRight(paneForCheckBoxes);
 36  
 37      EventHandler<ActionEvent> handler = e -> { 
 38        if (chkBold.isSelected() && chkItalic.isSelected()) {
 39          text.setFont(fontBoldItalic); // Both check boxes checked
 40        }
 41        else if (chkBold.isSelected()) {
 42          text.setFont(fontBold); // The Bold check box checked
 43        }
 44        else if (chkItalic.isSelected()) {
 45          text.setFont(fontItalic); // The Italic check box checked
 46        }      
 47        else {
 48          text.setFont(fontNormal); // Both check boxes unchecked
 49        }
 50      };
 51      
 52      chkBold.setOnAction(handler);
 53      chkItalic.setOnAction(handler);
 54      
 55      return pane; // Return a new pane
 56    }
 57    
 58    @Override // Override the start method in the Application class
 59    public void start(Stage primaryStage) {
 60      super.start(primaryStage);
 61      primaryStage.setTitle("CheckBoxDemo"); // Set the stage title
 62    }
 63    
 64    /**
 65     * The main method is only needed for the IDE with limited
 66     * JavaFX support. Not needed for running from the command line.
 67     */
 68    public static void main(String[] args) {
 69      launch(args);
 70    }
 71  }