
import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.beans.value.ObservableValue; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.Background; 
import javafx.scene.layout.BackgroundFill; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class TouchEventProblem extends Application { 

    // The button should go red when pressed, but on a Windows touch screen 
    // (e.g. Surface tablet) it only goes red if you touch the button and 
    // then move your finger. The bug is that it does not go red if you 
    // touch the button and keep your finger stationary. When you press the 
    // button with the mouse it goes red immediately, without moving the mouse. 

    // Note that on the console output a "touch pressed" event does occur 
    // when you touch the buttton with your finger, but this does not cause the 
    // button's pressed property to become true. When you press the button 
    // using the mouse, however, the "mouse pressed" event does seem sufficient 
    // to set the pressed property to true. 

    // Tested with Java 8u151 on Surface Pro 3 tablet running Windows 10 (version 1709) 
         
    @Override 
    public void start(Stage primaryStage) { 
        // create button 
        Button button = new Button(); 
        button.setPrefSize(200, 200); 

        // create two backgrounds 
        Background normalBackground = new Background(new BackgroundFill(Color.LIGHTBLUE, null, null)); 
        Background pressedBackground = new Background(new BackgroundFill(Color.RED, null, null)); 

        // bind backgrounds to pressed property 
        button.backgroundProperty().bind(Bindings 
                .when(button.pressedProperty()) 
                .then(pressedBackground) 
                .otherwise(normalBackground) 
        ); 
                
        // print out button's pressed property status 
        button.pressedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> { 
            System.out.println(System.currentTimeMillis() + ": PressedProperty = " + newValue); 
        }); 
         
        // print out touch events 
        button.setOnTouchPressed(e -> System.out.println(System.currentTimeMillis() + ": Touch Pressed event")); 
        button.setOnTouchReleased(e -> System.out.println(System.currentTimeMillis() + ": Touch Released event")); 

        // print out mouse events 
        button.setOnMousePressed(e -> System.out.println(System.currentTimeMillis() + ": Mouse Pressed event")); 
        button.setOnMouseReleased(e -> System.out.println(System.currentTimeMillis() + ": Mouse Released event")); 
                 
        // create stage 
        StackPane root = new StackPane(); 
        root.getChildren().add(button); 
        Scene scene = new Scene(root, 300, 250); 
        primaryStage.setTitle("TouchEventProblem"); 
        primaryStage.setScene(scene); 
        primaryStage.show(); 
    } 
    
    public static void main(String[] args) {
		launch(args);
	}

} 