【发布时间】:2016-02-19 21:41:29
【问题描述】:
我有一个上下文菜单,其中包含一个带有一些控件的 CustomMenuItem。单击按钮时,会显示一个对话框,并且上下文菜单会自动隐藏。这是问题所在:我想防止在对话框显示时关闭上下文菜单。我该如何解决这个问题?
我已经跟踪完成了该事件。当对话框打开时,会触发 FocusUngrabEvent.FOCUS_UNGRAB 事件,该事件在 PopupWindow 中进行处理。我已经尝试将 EventFilter 和 EventHandler 添加到 FocusUngrabEvent.FOCUS_UNGRAB 的上下文菜单并使用该事件,但这无济于事。
这里有一个 SSCCE 演示了这个问题:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class PimaryStage extends Application {
@Override
public void start(Stage primaryStage) {
// The button which shows the dialog.
Button button = new Button("Some Button");
button.setOnAction((event)-> {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText(null);
alert.setContentText("Some Text");
alert.show();
});
// Custom menu item which contains the button.
CustomMenuItem menuItem = new CustomMenuItem(button);
menuItem.setHideOnClick(false);
// Context menu.
ContextMenu menu = new ContextMenu();
menu.getItems().add(menuItem);
// Label.
Label label = new Label("Click here to open the context menu.");
label.setContextMenu(menu);
// Set the scene and show the stage.
Scene scene = new Scene(label, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
-
请谷歌搜索类似 setoncloserequests on window in javafx 的内容。你会找到你的解决方案,你可以删除问题后,这里已经问过了,所以你很可能会在这里看到答案
-
@checkMyProfile 你能给我一个提到的stackoverflow问题的链接吗?我找不到它
标签: javafx dialog contextmenu alert