调用事件处理程序 [英] Call event handler

查看:126
本文介绍了调用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带测试按钮的TabPane,调用此代码:

I have a TabPane with test button which calls this code:

Button bt1 = new Button("Select");

        bt1.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(final ActionEvent event)
            {
                TreeClass.getConnectedAgentsMap();

                TreePane.getTreeView().getSelectionModel().clearAndSelect(3);   

            }
        });

此代码选择TreeNode到TreeView中:

This code selects TreeNode into TreeView:

cell.setOnMouseClicked((MouseEvent me) ->
                {
                    if (!cell.isEmpty())
                    {
                        /// some action
                    }
                });

正如您所见,当鼠标选择树行时会触发此事件。
我尝试使用以下代码调用树单元格操作:

As you can see this event is triggered when mouse selects tree row. I tried to call the tree cell action with this code:

cell.selectedProperty().addListener(new ChangeListener<Boolean>()
                {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue)
                    {
                        /// Some Action

                    }
                });

但这不是正确使用它的方法,因为有几次打开了新的Tab。有没有办法当我点击按钮来调用动作事件?

But it's not the proper way to use it because several times new Tab is opened. Is there a way when I click the button to call action event?

推荐答案

final Point2D windowCoord = new Point2D(SCENE.getWindow().getX(), SCENE.getWindow().getY());
final Point2D sceneCoord = new Point2D(SCENE.getX(), Main.getStage().getScene().getY());
final Point2D nodeCoord = MYCONTROL.localToScene(0.0, 0.0);

final double x = Math.round(windowCoord.getX() + sceneCoord.getX() + nodeCoord.getX());
final double y = Math.round(windowCoord.getY() + sceneCoord.getY() + nodeCoord.getY());
try {
    Robot robot = new Robot();
    robot.mouseMove(new Double(x).intValue()+1, new Double(y).intValue());
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException ex) {
    ex.printStackTrace();
}

部分代码来自这个网站

这篇关于调用事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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