Java CheckMenuItem .selectedProperty ()

Syntax

CheckMenuItem.selectedProperty() has the following syntax.

public final BooleanProperty selectedProperty()

Example

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


/* www .j ava 2  s .  co  m*/
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

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");
        
        Image image = new Image(getClass().getResourceAsStream("a.png"));
        
        CheckMenuItem item = new CheckMenuItem("Item 1",new ImageView(image));
        tools.getItems().add(item);
        
        item.selectedProperty().addListener(new ChangeListener(){

          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
             
            
          }
          
        });
        
        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();
    }
}




















Home »
  Java Tutorial »
    javafx.scene.control »




Accordion
Button
CheckBox
CheckMenuItem
ChoiceBox
ComboBox
ContextMenu
Label
Menu
ProgressBar
ToggleButton
ToolBar
Tooltip
TreeItem
TreeView