JavaFX Tutorial - Java CheckMenuItem() Constructor








Syntax

CheckMenuItem() constructor from CheckMenuItem has the following syntax.

public CheckMenuItem()

Example

In the following code shows how to use CheckMenuItem.CheckMenuItem() constructor.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.CheckMenuItemBuilder;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/*from w w w . j  av  a2  s.c o m*/
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Menus");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        
        MenuBar menuBar = new MenuBar();
        
        Menu tools = new Menu("Your Menu");
        CheckMenuItem item = new CheckMenuItem();
        item.setText("Item 1");
        tools.getItems().add(item);
        
        tools.getItems().add(CheckMenuItemBuilder.create()
                .text("Item 2")
                .selected(true)
                .build());
        menuBar.getMenus().add(tools);
        
        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
        
        root.getChildren().add(menuBar); 
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}