Validation 使用EventFilters消费事件

Validation 使用EventFilters消费事件,validation,javafx,javafx-8,alert,eventfilter,Validation,Javafx,Javafx 8,Alert,Eventfilter,首先,有人能向我解释一下,为什么我的MouseEvent无论选择哪个Alert-选项都会被消耗掉?我想这与在EventFilter中调用Alert有关,但我还不清楚 public class EventFilterConsumeErrorExample extends Application { @Override public void start( final Stage primaryStage ) { final CheckBox checkBox = new Che

首先,有人能向我解释一下,为什么我的
MouseEvent
无论选择哪个
Alert
-选项都会被消耗掉?我想这与在
EventFilter
中调用
Alert
有关,但我还不清楚

public class EventFilterConsumeErrorExample extends Application
{
  @Override
  public void start( final Stage primaryStage )
  {
    final CheckBox checkBox = new CheckBox( "Check me!" );
    checkBox.setOnAction( ( event ) ->
    {
      System.out.println( "onAction: " + checkBox.isSelected() );
    } );

    checkBox.addEventFilter( MouseEvent.MOUSE_RELEASED, ( event ) ->
    {
      System.out.println( "onFilter" );

      final Alert alert = new Alert( AlertType.CONFIRMATION, "Do you wonna consume this Event?" );

      if ( alert.showAndWait().get().equals( ButtonType.OK ) )
      {
        System.out.println( "Yes, consume Event" );
        event.consume();
      }
      else
      {
        System.out.println( "No, do NOT consume Event" );
        //<-- Why is the Event consumed anyway? onAction won´t be called.
      }
    } );

    final BorderPane root = new BorderPane( checkBox );
    final Scene scene = new Scene( root, 400, 400 );
    primaryStage.setScene( scene );
    primaryStage.show();
  }


  public static void main( final String[] args )
  {
    System.out.println( "Java Version " + System.getProperties().get( "javafx.runtime.version" ) );
    launch( args );
  }
}
公共类EventFilterConsumeErrorExample扩展应用程序
{
@凌驾
公共作废开始(最终阶段初级阶段)
{
最终复选框=新复选框(“检查我!”);
checkBox.setOnAction((事件)->
{
System.out.println(“onAction:+checkBox.isSelected());
} );
复选框.addEventFilter(MouseEvent.MOUSE_已发布,(事件)->
{
System.out.println(“onFilter”);
最终警报=新警报(AlertType.CONFIRMATION,“您不会消费此事件吗?”);
if(alert.showAndWait().get().equals(ButtonType.OK))
{
System.out.println(“是,消费事件”);
event.consume();
}
其他的
{
System.out.println(“否,不使用事件”);

// 我对它进行了调试,我猜这是一个bug,你应该这样做。只要你留在EventFilter中,事件就不会被消耗。有时在处理代码之后,事件似乎在冒泡事件中被消耗。这可能是Alert类中嵌套事件的副作用

您可以通过始终使用事件并手动设置复选框来轻松解决该问题,例如

  if ( alert.showAndWait().get().equals( ButtonType.OK ) )
  {
    System.out.println( "Yes, consume Event" );
    event.consume();
  }
  else
  {
    System.out.println( "No, do NOT consume Event" );
    //<-- Why is the Event consumed anyway? onAction won´t be called.

    // always consume the event
    event.consume();

    // trigger the change manually
    checkBox.setSelected(!checkBox.isSelected());

  }
if(alert.showAndWait().get().equals(ButtonType.OK))
{
System.out.println(“是,消费事件”);
event.consume();
}
其他的
{
System.out.println(“否,不使用事件”);

//
jdk1.8.066
事件在这里没有被使用,
onAction
被称为Eird,我使用的是
jdk1.8.066
我自己。它对我来说在jdk1.8.071上运行得很好。@crusam顺便说一句-我正在使用Fedora 23谢谢你们的反馈。我正在使用Win7,我刚刚切换到最新的JDK版本(jdk1.8.074)这对我来说仍然不起作用。在这两种情况下,都没有调用setOnAction。感谢您确认,这可能是一个系统特定的错误。我开始认为这只是我们工作环境中的一个局部问题,因为其他声明,在他们的系统上它按预期工作。也感谢您的解决方案。只是讨厌,我在这种情况下,我必须解决一个bug,但至少它仍然会工作,当bug应该被修复时。我也已经考虑到了这一点。我提交了一份bug报告。有点惊讶,它不再在JIRA上;)。一旦它被批准为bug,我会在这里添加bug票证ID。