使用EventFilters消耗事件 [英] Consuming Events with EventFilters

查看:146
本文介绍了使用EventFilters消耗事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,任何人都可以向我解释,为什么我的 MouseEvent 会被消耗,无论我选择 Alert -Option ?我想这与在 EventFilter 中调用 Alert 有关,但这还不清楚。

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版本8.0.72-b15

onFilter

是,消费活动

onFilter

不,不要消耗事件

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

在这两种情况下,复选框都不会改变其状态,<$ c未调用$ c> onAction 。任何人都能解释为什么复选框没有正确处理事件?

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?

推荐答案

我调试了它,我的猜测是它是一个bug,你应该提交错误报告。只要您保留在EventFilter中,就不会消耗该事件。有时在处理完代码后,事件似乎在冒泡事件中消耗。这可能是Alert类中嵌套事件的副作用。

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.

您可以通过总是使用事件并手动设置复选框来轻松解决该问题,即。 e。

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

  }

然而,你有另一个问题,e。 G。如果用户通过键盘(空格键)触发复选框选择事件。但是你明确地捕获了鼠标事件,所以我把它留在那里(提示:你可以在例如onAction事件中恢复选择)。

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).

在Win7 x64上试过它Java 8u66和Java 8u74。

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

这篇关于使用EventFilters消耗事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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