import javafx.application.Application; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.MenuButton; import javafx.scene.control.MenuItem; import javafx.scene.control.NodeMenuItem; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { stage.setHeight(200.0); stage.setWidth(200.0); final Group root = new Group(); Scene scene = new Scene(root); final MenuButton sm = new MenuButton(); Rectangle rect = new Rectangle(30, 15); final NodeMenuItem nmi = new NodeMenuItem(rect); rect.setFill(Color.BLACK); rect.setOnMousePressed(new EventHandler() { public void handle(MouseEvent e) { System.out.println("FOO 3"); nmi.getOnAction().handle(null); sm.hide(); } }); nmi.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("FOO 2"); } }); sm.setText("Menu Button"); sm.getItems().addAll(nmi); sm.relocate(0, 20); root.getChildren().addAll(sm); stage.setScene(scene); stage.setVisible(true); } }