JavaFX2:上下文菜单的MODAL功能

时间:2013-09-19 19:33:58

标签: javafx-2 contextmenu modal-dialog

有没有办法将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();
}
}

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

此致

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是调用另一个舞台,并在显示舞台之前将modality设置为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();
    }
}