0

I develop a JavaFX application, which will be used on a system with touchscreen.

My development machine does not have a touchscreen. In this environment the app looks like a desktop application.

Is it possible to (manually) test touch based behaviour with JavaFX on a non-touch based device?

Is there some kind of emulator for JavaFX Applications which emulates touch events. (Similiar to android emulator for android apps?)

seeseost
  • 61
  • 2
  • 7

2 Answers2

2

You can construct a touch event with a constructor, then fire the event on a stage. You can add a filter on your stage for mouse events, consume them and fire a touch event in their stead. For example:

stage.addEventFilter(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
    mouseEvent.consume();
    Event touchEvent = new TouchEvent(lotsaparameters...);
    stage.fireEvent(touchEvent);
});
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • If you use cordinates outside the stage will there be an error or does it work? – Lealo Sep 28 '17 at 16:10
  • This is an event filter on the stage. It will filter events which are routed to the stage. Co-ordinates outside the stage won't be routed to the stage. If you press a mouse outside of the stage, as expected, there will be no error, because no error has occurred; nothing will happen because the event filter will not be triggered. – jewelsea Sep 28 '17 at 17:38
  • I would like whatever is outside the stage to be clicked, but that does not seem to happen – Lealo Sep 28 '17 at 17:50
  • Of course it doesn't. That isn't what this question asked or what this answer provides. If you have a new question, please ask it as a new question. – jewelsea Sep 28 '17 at 20:02
  • Remember that when you simulate the events programtically lke that, it is not the same as someone would touch that x,y position on the screen. If some controls mistakenly would be placed over eachother - it does not help at all to do it this way. As far as I can tell there is no thing such as awt.robot for touch events - as I am also looking to test touch events - I think there is a way to do this and that solution I think is related to being able to fire events outside the stage (without any target most prefferably). – Lealo Sep 28 '17 at 20:27
  • Check DVargas post (the above part) he explains this: https://stackoverflow.com/questions/40041625/how-to-fire-mouse-event-programmatically-in-javafx/40042513#comment79900468_40042513 – Lealo Sep 28 '17 at 20:29
0

Code that calls the event.source with a touch event

        button.setOnMouseClicked(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            TextField tf = (TextField) event.getSource();
            Stage stage = (Stage) tf.getScene().getWindow();
            double sceX = event.getSceneX();
            double sceY = event.getSceneY();
            double scrX = event.getScreenX();
            double scrY = event.getScreenY();
            TouchPoint tp = new TouchPoint(1, TouchPoint.State.PRESSED, sceX, sceY, scrX, scrY, (EventTarget) event.getSource(), null);                
            ArrayList<TouchPoint> tpa = new ArrayList<>();
            tpa.add(tp);
            tf.fireEvent(new TouchEvent(TouchEvent.TOUCH_PRESSED, 
                    tp,
                    tpa,
                    1,
                    false, 
                    false, 
                    false, 
                    false)
            );
            event.consume();
        }
    });
user176495
  • 124
  • 1
  • 2