仅在Swing应用程序中使用JavaFX触摸事件

时间:2016-05-04 02:59:03

标签: java swing javafx

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

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

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

1 个答案:

答案 0 :(得分:4)

This question on the FX mailing list表示不可能使用你所采用的方法,而是你需要创建一个JavaFX阶段并使用SwingNode(Swing in FX)而不是JFXPanel(Swing中的FX)嵌入你的Swing应用程序)。

我没有任何支持触控功能的硬件来测试这个,但我希望以下工作能够正常运行......

<!-- HTML markup -->

<div id = "login">
    <p>User <input type = "text" id = "username"></p>
    <p>Password <input type = "password" id = "password"></p>
    <button>Log In</button>  
</div>


<!-- method1 using script tag -->

<script>
  var loginButton = document.querySelector('#login button');
  var username,password;

  loginButton.addEventListener('click',function(){
      username = document.getElementById('username').value;
      password = document.getElementById('password').value;

      alert('username: ' + username + '\n password : ' + password);
  });

</script>


<!-- 
     method2 include a login.js script(containing the previous js code)    
     to benefit from caching when refreshing or visiting 
     the page in the next time 
-->

<script src="login.js"></script>