Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use only JavaFX touch events in Swing Application

Tags:

java

swing

javafx

Is there a way to use the JavaFX touch events in a swing application? Currently I am using a JFXPanel to capture the JavaFX events, however when I try to get the events I am not receving any touch events and only mouse events instead. This is tested on a Windows 8.1 Dell Touch Screen.

Updated: The code below is the skeleton of what I am using to get the events. This JFXPanel is used as a glasspane in the Swing application. This creates a JFXPanel for the glasspane, which is able to capture all the events.

public class MouseEventRouter extends JFXPanel {
    ...

    public ZeusMouseEventRouter(JMenuBar menuBar, Container contentPane) {
        ...
        this._contentPane = contentPane;
        this._contentPane.add(_JFXpanel);
        this._contentPane.setVisible(true);
        init();
    }

    private void init() {
        pane = new VBox();
        pane.setAlignment(Pos.CENTER);
        Platform.runLater(this::createScene);
    }

    private void createScene() {
        Scene scene = new Scene(pane);
        ...

        scene.setOnTouchPressed(new EventHandler<javafx.scene.input.TouchEvent>() {
            @Override public void handle(javafx.scene.input.TouchEvent event) {
                System.out.println("tap down detected");
            }
        });

        ...
        setScene(scene);
    }
}
like image 802
Nick Avatar asked May 04 '16 02:05

Nick


1 Answers

This question on the FX mailing list suggests it is not possible using the approach you've taken, instead you'll need to create a JavaFX stage and embed your Swing application using SwingNode (Swing in FX) instead of JFXPanel (FX in Swing).

I've not got any touch enabled hardware to test this, but I expect the following to work...

public class TouchApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        JPanel swingContent = new JPanel();
        swingContent.add(new JButton("Hello world"));
        swingContent.add(new JScrollBar());

        BorderPane content = new BorderPane();
        SwingNode swingNode = new SwingNode();
        swingNode.setContent(swingContent);
        content.setCenter(swingNode);
        Scene scene = new Scene(content);
        scene.setOnTouchPressed((e) -> {
            System.out.println(e);
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
like image 98
Adam Avatar answered Oct 21 '22 08:10

Adam