THe_strOX
THe_strOX

Reputation: 717

Why is Mouse Pressed Event not working in TextArea of JavaFX?

This is my code:

@FXML private TextArea txtconfig;
public void handleMousePressed(MouseEvent mouseEvent) {
        txtconfig.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("PRESSED");
            }
        });
    }

In FXML:

TextArea fx:id="txtconfig"  style="-fx-font-size:10pt; -fx-font-family:Consolas"
              maxWidth="Infinity" maxHeight="Infinity" VBox.vgrow="ALWAYS"
              onMousePressed="#handleMousePressed"/>

However, setOnMouseClicked is working keeping everything same. Only, onMousePressed is changed to onMouseClicked in FXML.

public void handleMouseClicked(MouseEvent mouseEvent) {
        txtconfig.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                 System.out.println("Clicked");
            }
        });

    }

Upvotes: 1

Views: 1686

Answers (1)

Markus Weninger
Markus Weninger

Reputation: 12648

As suggested in this answer you could install an ÈventFilter if you really need the MousePressed event (which seems to be consumed by the control itself):

txtconfig.addEventFilter(
    MouseEvent.MOUSE_PRESSED,
    new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            System.out.println("PRESSED");
        }
    }
);

Upvotes: 1

Related Questions