/* * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. */ package tests; 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.stage.Stage; public class TestPopupAutoHide extends Application { public TestPopupAutoHide() { super(); } public void start(Stage stage) { stage.setTitle("Sample"); Scene scene = new Scene(new Group(), 800, 500); final MenuItem myMenu = new MenuItem("My Menu"); final Menu mainMenu = new Menu("Main"); mainMenu.getItems().addAll(myMenu); /////////////////////////////////////// final MenuBar menuBar = new MenuBar(); menuBar.setUseSystemMenuBar(true); menuBar.getMenus().add(mainMenu); final ContextMenu contextMenu = new ContextMenu(); MenuItem item1 = new MenuItem("About"); item1.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("About"); } }); MenuItem item2 = new MenuItem("Preferences"); item2.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("Preferences"); } }); contextMenu.getItems().addAll(item1, item2); contextMenu.setAutoHide(true); final TextField textField = new TextField("Type Something"); textField.setLayoutX(50); textField.setLayoutY(100); final Label label = new Label("my label"); label.setContextMenu(contextMenu); label.setLayoutX(50); label.setLayoutY(200); Group group = (Group) scene.getRoot(); group.getChildren().clear(); group.getChildren().addAll(menuBar, textField, label); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } }