为什么Mouse Pressed Event在JavaFX的TextArea中不起作用?

时间:2017-07-25 12:14:31

标签: javafx javafx-8

这是我的代码:

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

在FXML中:

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

然而,setOnMouseClicked正在努力保持一切相同。只有onMousePressed在FXML中更改为onMouseClicked。

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

    }

1 个答案:

答案 0 :(得分:0)

正如this answer中所建议的那样,如果你确实需要ÈventFilter事件(这似乎是由控件本身使用),你可以安装MousePressed

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