JavaFx 从节点调用事件处理程序

JavaFx call event handler from node

我正在尝试为 javaFx 中的特殊需要做一个自定义快捷方式系统。

这种特殊需求导致无法使用KeyCombinaison(不能接受只有一个键+修饰符的限制)。

我已经完成了正确的 KeyCombinaison 系统,现在我想从节点调用一个处理程序(我在控制器之外)。但是我找不到任何优雅的解决方案来执行此操作。

有按钮声明:

<ToggleButton fx:id="selective1Button"
    text="Sélectif 1"
    onMousePressed="#handleSelective1ButtonPressAction"
    onMouseReleased="#handleSelective1ButtonReleaseAction"/>

然后我想从我的快捷方式代码中调用控制器的操作。

public class Shortcut() {

    public void test() {
        ToggleButton button = (ToggleButton) scene.lookup("#selective1Button");
        // How to call #handleSelective1ButtonPressAction from here ? 
    }
}

还有一个标准控制器。

public class MyController {

    // ....
    @FXML
    private ToggleButton selective1Button;

    @FXML
    public void handleSelective1ButtonPressAction() {
        selective1Button.setSelected(true);
    }
}

我可以做一些工作,例如使用 java.Robot 并模拟一些点击,但这不是很漂亮。

他们还有别的办法吗?

谢谢

我的问题是我想调用鼠标事件。这可以通过使用 Robot class 来存档:Example of usage of Robot

但是,可以使用标准操作,并且 link 通过调用方法 fire() 将它们与按钮一起使用:

public void test() {
        ToggleButton button = (ToggleButton) scene.lookup("#selective1Button");
        button.fire();
    }

正如jewelsea所说,使用mousePressed和mouseRelease不是很标准。