区分真正的鼠标点击火方法

时间:2016-05-27 10:30:44

标签: java javafx hyperlink

我正在使用javaFX来创建一个应用程序。 我在某个地方有一个超链接,我为它设置了一个(onAction),如下所示

Hyperlink studentList = ...;  // It's given proper object
studentList.setOnAction(...);

现在在其他地方我用这种方法来模拟点击这个超链接

studentList.fire();

现在我的问题是如何区分真正的click / keyPress和fire()方法?

1 个答案:

答案 0 :(得分:1)

这是一种方法。只需在EventHandler媒体资源中添加setOnMousePressed即可。请务必将其添加到setOnMousePressed,而不是{ setOnMouseClicked,因为setOnMousePressed在调用fire()之前调用,而setOnMouseClicked之后调用import javafx.application.Application; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Hyperlink; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class MCVE extends Application { @Override public void start(Stage stage) { VBox content = new VBox(5); content.setPadding(new Insets(10)); Hyperlink link = new Hyperlink("Hyperlink"); Button fireButton = new Button("Fire hyperlink"); fireButton.setOnAction(e -> link.fire()); BooleanProperty mouseClicked = new SimpleBooleanProperty(false); link.setOnMousePressed(e -> { System.out.println("Mouse click"); mouseClicked.set(true); }); link.setOnAction(e -> { if (!mouseClicked.get()) { System.out.println("No mouse click"); } mouseClicked.set(false); }); content.getChildren().addAll(link, fireButton); stage.setScene(new Scene(content)); stage.show(); } public static void main(String[] args) { launch(); } }

strongSelf.messages = nil