Consuming Events with EventFilters

橙三吉。 提交于 2019-12-20 04:15:43

问题


First, can anyone explain to me, why my MouseEvent gets consumed no matter which Alert-Option I choose? I guess it has something to do with calling an Alert within an EventFilter, but thats just not yet clear to me.

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

Programm generates following output, after clicking the checkbox twice and click each dialog decision once:

Java Version 8.0.72-b15
onFilter
Yes, consume Event
onFilter
No, do NOT consume Event

In both cases the checkbox does not change its state, onAction is not called. Anyone able to explain why the event is not properly handled by the checkbox?

Second, how would you avoid the checkbox of beeing checked on a user-based decision?


回答1:


I debugged it and my guess is that it's a bug, you should file a bug report. The event isn't consumed as long as you remain in the EventFilter. Sometimes after that code was processed, the event appears to be consumed in the bubbling event. That's probably a side-effect of the nested eventing in the Alert class.

You can solve that problem easily by always consuming the event and setting the checkbox manually, i. e.

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

  }

However, you got another problem there, e. g. if a user triggers the checkbox selection event by keyboard (space bar). But you explicitly caught the mouse event, so I leave it at that (hint: you could e. g. revert the selection in the onAction event).

Tried it on Win7 x64 with Java 8u66 and Java 8u74.



来源:https://stackoverflow.com/questions/35922390/consuming-events-with-eventfilters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!