John
John

Reputation: 383

How to create MouseClick event to detect which node is clicked? - javafx

I want to do global MouseClick event to detect which node is clicked in JavaFX. I mean when someone will click a button then event.getSource will return me reference to this button.

Any ideas, how can i do this?

Upvotes: 1

Views: 1751

Answers (1)

WillBD
WillBD

Reputation: 1919

One way to go about this would be to have a static variable somewhere that was of type Node, and then in the listener of the button, just assign a reference to the button in the handler to that button. ex:

public Class test {
  public static Node whichClick;

  myButton.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent e){
      whichClick = myButton;                        
    }
  });
}

And then you could access that variable from wherever.

Upvotes: 2

Related Questions