Wednesday, February 8, 2012

JavaFX 2.0: Ckeck Menu

Ckeck Menu is a MenuItem that can be toggled between selected and unselected states.
JavaFX 2.0: Ckeck Menu
package javafx_exmenu;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_exMenu extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

//Top Menu Bar
MenuBar menuBar = new MenuBar();

Menu menu1 = new Menu("Menu");

MenuItem menuItemA = new MenuItem("Item A");
menuItemA.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item A Clicked");
}
});

MenuItem menuItemB = new MenuItem("Item B");
menuItemB.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item B Clicked");
}
});

MenuItem menuItemC = new MenuItem("Item C");
menuItemC.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item C Clicked");
}
});

//Sub-Menu
Menu subMenu_s1 = new Menu("SubMenu 1");
MenuItem subMenuItem1 = new MenuItem("Sub Menu Item 1");
MenuItem subMenuItem2 = new MenuItem("Sub Menu Item 2");
MenuItem subMenuItem3 = new MenuItem("Sub Menu Item 3");
subMenu_s1.getItems().addAll(subMenuItem1, subMenuItem2, subMenuItem3);

//Check Menu
Menu checkMenu = new Menu("Check Menu");
checkMenu.getItems().add(
CheckMenuItemBuilder.create()
.text("Check Items 1")
.build());
checkMenu.getItems().add(
CheckMenuItemBuilder.create()
.text("Check Items 2")
.selected(true)
.build());


menu1.getItems().add(menuItemA);
menu1.getItems().add(menuItemB);
menu1.getItems().add(menuItemC);
menu1.getItems().add(subMenu_s1); //Add Sub-Menu

menuBar.getMenus().add(menu1);
menuBar.getMenus().add(checkMenu); //Add Check Menu

menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
}

No comments:

Post a Comment