简体   繁体   English

JavaFX图表上的Swing上下文菜单

[英]Swing context menu on JavaFX Chart

I am working on an application primarily in swing, though the chart dialog uses JavaFX. 尽管图表对话框使用JavaFX,但我主要在摆动应用程序。 ( Chart in a GridPane in a JFXPanel to be exact.) ChartGridPaneJFXPanel要准确。)

First, Chart does not implement a setContextMenu method to use a JFX ContextMenu . 首先, Chart没有实现setContextMenu方法来使用JFX ContextMenu I have seen a workaround by attaching a handler to the right click of the chart, but the menu is not removed from the GUI when it loses focus, rather it persists until the user either 1) triggers it to open again or 2) clicks a MenuItem . 我已经看到了通过在图表的右键单击上附加处理程序的解决方法,但是当菜单失去焦点时,菜单不会从GUI中删除,而是一直存在,直到用户1)触发它再次打开或2)单击a。 MenuItem

chart.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        if (t.getButton() == MouseButton.SECONDARY) {
            contextMenu.show(chart, t.getScreenX(), t.getScreenY());
        }
    }
});

That said, I would prefer to use a Swing JPopupMenu instead. 也就是说,我宁愿使用Swing JPopupMenu How can I bind this to my Chart and maintain a natural behavior? 如何将其绑定到Chart并保持自然行为?

Thank you! 谢谢!

I determined it was not possible to use a JPopupMenu . 我确定不可能使用JPopupMenu

The ContextMenu is added to a JLabel : ContextMenu已添加到JLabel

Label contextMenuLabel = new Label();
grid.add(contextMenuLabel, 0, 0); // Add to the same grid after adding the chart
contextMenuLabel.setPrefSize(width, height);
contextMenuLabel.setMinSize(width, height);
contextMenuLabel.setMaxSize(width, height);

ContextMenu contextMenu = new ContextMenu();

contextMenuLabel.setContextMenu(contextMenu);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM