2014-08-28 232 views
0

所以我有这个JavaFX应用程序,其中包含一个按钮,应该打开DirectoryChooser onclick。当我触发它一次,它做它应该做的事,非常好。只要我关闭DirectoryChooser对话框,该按钮不会再做任何事情。我在网上搜索一些“事件重置”或类似的东西,因为我想也许该事件仍然“活跃”,因此不会再触发,但没有任何结果:JavaFX按钮事件只触发一次

// first attempt 
button.addEventFilter(MouseEvent.MOUSE_CLICKED, 
    new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent e) { 
      // dirChooser.setTitle("Select Directory:"); 
      file = dirChooser.showDialog(primaryStage); 

      // just incase only the DirectoryChooser wasn't opening 
      System.out.println("asdf"); 

      // updates the application view with the new selected path 
      update(); 

      // not sure, if this affects anything 
      // found it while looking for resetting of events 
      e.consume(); 
     }; 
    } 
); 
// secont attempt 
button.setOnAction(new EventHandler<ActionEvent>() { 
    @Override public void handle(ActionEvent e) { 
     DirectoryChooser dirChooser = new DirectoryChooser(); 
     dirChooser.setTitle("Select Directory:"); 
     file = dirChooser.showDialog(primaryStage); 
     update(); 
    } 
}); 

不知道这只是一个完全错误的方法,或者如果我错过了重要的东西。我希望你们能弄明白。

+0

你是什么意思的“按钮不再做任何事情”?第二次和以后的点击没有触发事件处理程序? – 2014-08-28 10:47:45

+0

那么,DiaglogChooser没有打开,没有控制台输出,似乎没有调用句柄函数。所以是的,它没有被触发。 – 2014-08-28 11:09:21

+0

为了调试目的,你可以注释掉代码中的dirChooser(和update())行,然后再次点击。有没有控制台输出? – 2014-08-28 11:22:21

回答

0

所以在Uluk Biy的帮助下,我得到了这个问题。在更新例程中,该按钮是新创建的,因此它的事件处理程序不再存在。我将这个按钮添加到属性中,所以我不需要每次调用更新例程时都替换它,并且事件处理程序仍然存在。