ADVERTISEMENT

Solved! JavaFX Label not Responding to Click Event

The JavaFX Label is part of the JavaFX library that is used to display uneditable text. Under this article, we are going to offer a solution to JavaFX Label not responding to click events or JavaFx Label not clickable even after applying click event.

How to Solve JavaFX not responding to click events

There is only one scenario when JavaFX Label may not be clickable; only when the wrong event is passed to the method handling event. Label click event passes MouseEvent, if you provided ActionEvent as an event handler, then you will get class cast exception because the handler created by the FXML loader passes the event object as a MouseEvent.

An Example of JavaFX Label Click Event

Label label = new Label(); 
label.setOnMouseClicked(e ->{
	System.out.println("label has been clicked");
});

Click Event from FXML File

<Label fx:id="myLabel" onMouseClicked="#printMessage" ... />

@FXML
private void printMessage(MouseEvent event) {
    System.out.println("label has been clicked");
}