JavaFX:PopOver窗口(ControlsFX)的鼠标事件 [英] JavaFX : Mouse events for a PopOver Window (ControlsFX)

查看:95
本文介绍了JavaFX:PopOver窗口(ControlsFX)的鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来显示 PopOver (通过 ControlsFX 自定义 PopUp -

I am having the following code to display a PopOver (Custom PopUp by ControlsFX - mvn repo)

public class JavaFXApplication35 extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {

            Label lblName = new Label("Tetsing name");
            Label lblStreet = new Label("Some street name");
            Label lblCityStateZip = new Label("Some city, 111111");
            VBox vBox = new VBox(lblName, lblStreet, lblCityStateZip);

            PopOver popOver = new PopOver(vBox);

            Label label = new Label("Mouse mouse over me");

            label.setOnMouseEntered(mouseEvent -> {
                popOver.show(label, -3);
            });

            label.setOnMouseExited(mouseEvent -> {
                if (popOver.isShowing()) {
                    popOver.hide();
                }
            });

            StackPane root = new StackPane();
            root.getChildren().add(label);

            Scene scene = new Scene(root, 300, 250);

            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.setOnCloseRequest((WindowEvent event) -> {
                System.exit(0);
            });
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

问题是,

  1. 我希望在鼠标进入 Label 时显示弹出窗口-可以正常工作.

  1. I want the pop-up to be displayed when mouse entered the Label - works fine.

我希望在用户从 Label 退出鼠标时隐藏弹出窗口,但如果他在弹出窗口中输入鼠标,则不希望弹出窗口.

I want the pop-up to be hidden when user exits mouse from Label but not if he enters mouse in to the pop-up window.

我在Label上添加了 MouseEntered MouseExited 操作,但是如果用户输入鼠标,我又不想隐藏弹出窗口,该如何处理?进入弹出窗口.

I have added MouseEntered and MouseExited actions on Label but how can i handle the another scenario where i don't want to hide the pop-up if user enters mouse in to pop-up.

推荐答案

我遇到了同样的问题.这是我的解决方案.只需将标签(或其他节点)和PopOver的内容节点作为此方法的参数传递即可.

I ran into the same problem. Here is my solution. Just pass your label (or other node) and PopOver's content node as arguments to this method.

public static void addAutoHidingPopOver(Node hoverableNode, Node contentNode) {
   //Creating PopOver
   PopOver popOver = new PopOver(hoverableNode);
   popOver.setContentNode(contentNode);
   //Here you can set custom parameters of your PopOver
   //...

   //Mouse Actions handling
   final Timeline timeline = new Timeline();
   timeline.getKeyFrames().add(new KeyFrame(Duration.millis(1000)));
   timeline.setOnFinished(finishEvent -> {
        if (hoverableNode.isHover() || contentNode.isHover()) timeline.play();
        else popOver.hide();
   });
   hoverableNode.setOnMouseEntered(mouseEvent -> {if (!popOver.isShowing()) popOver.show(hoverableNode);});
   hoverableNode.setOnMouseExited(mouseEvent -> timeline.play());
}

鼠标离开hoverableNode或contentNode 1秒后,PopOver将被隐藏.像这样使用它:

PopOver will be hidden after 1 sec after mouse leave hoverableNode or contentNode. Use it like this:

addAutoHidingPopOver(someLabel, someContentNode);

请注意,您的内容节点应占用PopOver的所有可见空间以方便使用.

Note, that your content node should take all visible space of PopOver for comfort use.

这篇关于JavaFX:PopOver窗口(ControlsFX)的鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆