Search code examples
javafxjavafx-8fxml

JavaFx call event handler from node


I am trying to do one custom system of shortcuts for special need in javaFx.

This special need make it's not possible to use KeyCombinaison (limitation of only one key + modifier is not acceptable).

I have do my proper KeyCombinaison system and now I want to call one handler from the node (I'm outside the controller). But i can't find any elegant solution to perform this.

There is the button declaration :

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

And after I want to call the action of my controller from my shortcut code.

public class Shortcut() {

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

And a standard controller.

public class MyController {

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

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

I can do something working, for example by using java.Robot and simulate some click, but that's not really pretty.

Are they any another way ?

Thanks


Solution

  • My problem was I wanted to invoke mouse event. This can be archived with using Robot class : Example of usage of Robot

    But instead, is possible to use standard action and link them with the button by call the method fire() :

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

    As jewelsea say, using mousePressed and mouseRelease is not very standard.