0

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

Manticore
  • 441
  • 5
  • 24
  • It is pretty unclear to me what you are asking. Perhaps you want to use a [mnemonic or an accelerator](http://stackoverflow.com/questions/12710468/using-javafx-2-2-mnemonic)? You might want to explain what your special need is and what you are trying to accomplish. Looking up the button by id is strange - it has an `fx:id` so you already have a reference to the button in your code. Handlers on toggle button press and release. – jewelsea May 27 '15 at 09:37
  • I have special mnemonic for a special need of the project. I have extends the KeyCombinaison. Now I search a way to call the handler from the node (I'm outside the controller). – Manticore May 27 '15 at 09:46
  • It still seems like an accelerator is the appropriate solution for you. My previous comment got cut, but I was wondering why you have handlers for mouse press and release - that is quite unusual for a button. – jewelsea May 27 '15 at 09:50
  • Accelerator need a KeyCombinaison, but I can't use KeyCombinaison. Because I need to bypass the limit of only one key + modifier for specials projects needs. But you have right, use mouse press and release cause trouble. I have change to onAction, and now button.fire() works fine ;) – Manticore May 27 '15 at 10:08
  • I have edited my question. If you want you can post an answer and I will accept them. – Manticore May 27 '15 at 10:14

1 Answers1

0

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.

Community
  • 1
  • 1
Manticore
  • 441
  • 5
  • 24