java - Swing context menu on JavaFX Chart
问题描述
I am working on an application primarily in swing, though the chart dialog uses JavaFX. (Chart
in a GridPane
in a JFXPanel
to be exact.)
First, Chart
does not implement a setContextMenu
method to use a 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
.
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. How can I bind this to my Chart
and maintain a natural behavior?
Thank you!
解决方案
我确定不可能使用JPopupMenu
.
被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);
推荐阅读
- reactjs - 使用单个操作修改多个值或每个值的操作的优缺点是什么?为什么?
- html - ATOM 文本编辑器文本颜色因哈希而改变
- java - 有没有办法从特定行的列中获取值并将其放入下一行?
- java - aui 验证器无法处理 liferay 中的表单提交
- javascript - 有没有办法在 laracast flash 中改变生命时间?
- python - 如何根据连续索引拆分数据框?
- ios - 应用程序在后台时没有立即调用 DidRecieveRemoteNotification
- java - 给jvm添加不同的p12证书
- c# - 是否有不改变输出日期值的日期时间格式?
- java - 从 Sharedpreference 获取数据时尝试在空对象引用上调用虚拟方法