2015-05-27 53 views
0

我试图在javaFx中为特殊需要执行一个自定义系统快捷方式。来自节点的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和模拟一些点击,但这不是很漂亮。

他们有没有别的办法?

谢谢

+0

我对你的要求很不清楚。也许你想使用[助记符或加速器](http://stackoverflow.com/questions/12710468/using-javafx-2-2-monemonic)?你可能想解释你的特殊需求是什么以及你想要完成什么。通过id查找按钮很奇怪 - 它有一个'fx:id',因此您已经在代码中引用了该按钮。处理器按住切换按钮并释放。 – jewelsea

+0

我对项目的特殊需求有特殊的助记符。我已经扩展了KeyCombinaison。现在我搜索一种方式从节点调用处理程序(我在控制器之外)。 – Manticore

+0

它仍然看起来像一个加速器是适合你的解决方案。我以前的评论被削减,但我想知道为什么你有处理鼠标按下和释放 - 这是非常不寻常的按钮。 – jewelsea

回答

0

我的问题是我想调用鼠标事件。这可以通过使用机器人类归档:Example of usage of Robot

而是可以使用标准动作,并与按钮通过调用该方法火()将它们链接:

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

由于jewelsea说,使用的mousePressed和mouseRelease不是很标准。