generating a MouseEvent in JavaFX

霸气de小男生 提交于 2019-12-21 11:02:44

问题


I'm in need of simulating a MouseEvent.MOUSE_CLICKED. I want to use the fireEvent method of a particular Node in order to dispatch an event of the aforementioned type. However, I am struggling with generating one. It appears that javafx.scene.input.MouseEvent has no valid constructor, but old java.awt.event.MouseEvent objects can be instantiated this way. Still, I haven't found any working solution for conversion. How do I go around this?

Thanks.


回答1:


You can generate a MouseEvent using the deprecated MouseEvent.impl_mouseEvent API. I did this previously in this forum thread for JavaFX 2.0. Note that the API is deprecated for a reason - it is private API used in the implementation of JavaFX and the API is not guaranteed to maintain the same signature or even exist in future versions (which can be evidenced because the original code I posted in the forum thread no longer compiles.

The correct solution to generating such an event is to have a public API so support this. There has already been a request filed to supply this functionality RT-9383 "Add proper constructors & factory methods to event classes, remove impl". This jira is scheduled to be completed next year for JavaFX 3.0.

In the meantime, usage of the Robot class as Sergey suggests is probably your best method.


Update: Java 8 added public constructors for javafx.event.MouseEvent and the (as indicated in Jay Thakkar's answer), you can fire such an event using Event.fireEvent (you can also fire events on Windows).




回答2:


This will fire a single primary mouse click at your node:

import javafx.event.Event; 
import javafx.scene.input.MouseButton; 
import javafx.scene.input.MouseEvent;

Event.fireEvent(YOUR NODE, new MouseEvent(MouseEvent.MOUSE_CLICKED, 0,
                0, 0, 0, MouseButton.PRIMARY, 1, true, true, true, true,
                true, true, true, true, true, true, null));



回答3:


Or you can use a simple "hack" to do a programmatic click on the button. Create this method in a "Util" class:

public static void click(javafx.scene.control.Control control) {
    java.awt.Point originalLocation = java.awt.MouseInfo.getPointerInfo().getLocation();
    javafx.geometry.Point2D buttonLocation = control.localToScreen(control.getLayoutBounds().getMinX(), control.getLayoutBounds().getMinY());
    try {
        java.awt.Robot robot = new java.awt.Robot();
        robot.mouseMove((int)buttonLocation.getX(), (int)buttonLocation.getY());
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.mouseMove((int) originalLocation.getX(), (int)originalLocation.getY());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Then, to "click" on the button just call the method click passing your button as parameter.




回答4:


When you set a handler, it sets a public property. You can get the event from that property and call handle():

button1.setOnMouseClicked()....
the corresponding property is
button1.onMouseClickedProperty().get().handle(me);//where me is some MouseEvent object


来源:https://stackoverflow.com/questions/11552176/generating-a-mouseevent-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!