1

有没有办法将 MODAL 功能添加到上下文菜单?

我的代码如下:

package snippet;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class ContextMenuSample extends Application
{
public static void main(String[] args)
{
    launch(args);
}

@Override
public void start(Stage stage)
{
    stage.setTitle("ContextMenuSample");

    Scene scene = new Scene(new Group(), 450, 250);

    Label toLabel = new Label("To: ");

    TextField notification = new TextField();

    final ContextMenu contextMenu = new ContextMenu();
    contextMenu.setAutoHide(false);
    contextMenu.setOnShowing(new EventHandler<WindowEvent>()
    {
        public void handle(WindowEvent e)
        {
            System.out.println("showing the context menu");
        }
    });
    contextMenu.setOnShown(new EventHandler<WindowEvent>()
    {
        public void handle(WindowEvent e)
        {
            System.out.println("context menu has been shown");
        }
    });
    MenuItem closeItem = new MenuItem("Close");
    closeItem.setOnAction(new EventHandler<ActionEvent>()
    {
        public void handle(ActionEvent e)
        {
            contextMenu.hide();
        }
    });
    MenuItem colorItem = new MenuItem("Choose", new ColorPicker());
    colorItem.setOnAction(new EventHandler<ActionEvent>()
    {
        public void handle(ActionEvent e)
        {
            System.out.println("Preferences");
        }
    });


    GridPane contextGridPane = new GridPane();

    Pane pane = new Pane();
    pane.getChildren().add(contextGridPane);


    contextMenu.getItems().addAll(colorItem, deleteItem// , subsystem1,
                                                       // radioItem
        );
    toLabel.setContextMenu(contextMenu);
    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(toLabel, 0, 0);
    grid.add(notification, 1, 0);
    grid.add(new ColorPicker(), 2, 0);
    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
}
}

当用户点击标签“To”时,会出现一个上下文菜单。我希望此上下文菜单具有模态功能,以便用户无法在应用程序上执行任何其他操作,除非在上下文菜单上执行某些操作。此外,当上下文菜单处于活动状态时,用户应该无法单击应用程序上的其他任何位置。

问候,

4

1 回答 1

1

最简单的解决方案是调用另一个 Stage 并在显示阶段之前使用initModality设置其模态。据我了解,您可能想使用。 Modality.APPLICATION_MODEL

这是一个源自您的小示例(顺便说一句,您的代码甚至无法运行,它有错误)

public class ContextMenuSample extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(final Stage stageOne)
    {
        final Stage stageTwo = new Stage();
        stageTwo.initModality(Modality.APPLICATION_MODAL);

        final Pane layoutOne = new HBox(10);
        Pane layoutTwo = new HBox(10);

        Label labelOne = new Label("click");
        Label labelTwo = new Label("other click");

        labelOne.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                stageTwo.show();
            }
        });

        labelTwo.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                stageTwo.close();
            }
        });

        Scene sceneOne = new Scene(layoutOne);
        Scene sceneTwo = new Scene(layoutTwo);

        layoutOne.getChildren().add(labelOne);
        layoutTwo.getChildren().add(labelTwo);

        stageOne.setScene(sceneOne);
        stageTwo.setScene(sceneTwo);

        stageOne.show();
    }
}
于 2013-09-19T20:56:42.310 回答