JavaFX任务中的上下文菜单

时间:2014-03-10 23:38:25

标签: javafx javafx-2 javafx-8

关注此主题Context menu insight JavaFX Task我想在JavaFX Task中创建Context Menu。我测试了这段代码:

static private StringBuilder stringBuilder = new StringBuilder();

    private static ContextMenu contextMenu;
    private static CountDownLatch menuCreated = new CountDownLatch(1);

    static synchronized void writeString(String s)
    {
        stringBuilder.append(s).append("\n");
    }

    public static BorderPane init(BorderPane bp) throws Exception
    {

        System.out.println("***** CALLED");

        Task task = new Task()
        {
            @Override
            protected Void call() throws Exception
            {
                writeString("Task started");

                writeString(Thread.currentThread().getName() + " is fx thread: "
                    + Platform.isFxApplicationThread());

                Platform.runLater(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        writeString(Thread.currentThread().getName() + " is fx thread: "
                            + Platform.isFxApplicationThread());
                        try
                        {
                            contextMenu = new ContextMenu();
                            contextMenu.setId("Test ID");
                            writeString("Created context menu");
                            menuCreated.countDown();
                        }
                        catch (Exception ex)
                        {
                            writeString(ex.getMessage());
                            ex.printStackTrace();
                        }
                        finally
                        {
                            writeString("Test");
                        }
                    }
                });

                writeString("Task finished");
                return null;
            }
        };

        new Thread(task).start();

        MenuItem item1 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("About");
            }
        });
        MenuItem item2 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Preferences");
            }
        });
        MenuItem item3 = new MenuItem("Close");
        item3.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                //flow.getChildren().remove(bp);
            }
        });
        contextMenu.getItems().addAll(item1, item2, item3);

        bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
        {
            @Override
            public void handle(ContextMenuEvent event)
            {
                //contextMenu.hide();

                System.out.println("*********************** Shown Context Menu ***!!!!!!!");
                contextMenu.show(bp, event.getScreenX(), event.getScreenY());
                event.consume();
            }
        });

        bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                contextMenu.hide();
            }
        });

        menuCreated.await();

        return bp;
    }

使用此代码,我为BorderPane设置了Context Menu。当我用鼠标右键单击时,我看到调试消息***********************显示的上下文菜单*** !!!!!!!但是没有上下文菜单。你能帮我解决一下这段代码吗?

0 个答案:

没有答案