仅在Swing应用程序中使用JavaFX触摸事件 [英] Use only JavaFX touch events in Swing Application

查看:121
本文介绍了仅在Swing应用程序中使用JavaFX触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在swing应用程序中使用JavaFX触摸事件?目前我正在使用JFXPanel捕获JavaFX事件,但是当我尝试获取事件时,我不会接收任何触摸事件而只接收鼠标事件。这是在Windows 8.1 Dell触摸屏上测试的。

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.

更新:以下代码是我用来获取事件的框架。此JFXPanel用作Swing应用程序中的glasspane。这为glasspane创建了一个JFXPanel,它能够捕获所有事件。

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);
    }
}


推荐答案

< a href =http://mail.openjdk.java.net/pipermail/openjfx-dev/2014-May/014008.html\"rel =nofollow> FX邮件列表上的这个问题表明它是不可能使用你采用的方法,而是你需要创建一个JavaFX阶段并使用SwingNode(Swing in FX)而不是JFXPanel(Swing中的FX)嵌入你的Swing应用程序。

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();
    }
}

这篇关于仅在Swing应用程序中使用JavaFX触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆