0

当我将上下文菜单添加到树表视图时,不幸的是,它仍然会对上下文菜单未使用的 KeyEvents 做出反应。

在此处输入图像描述

例如,单击一个子项(Item22)打开其上下文菜单,然后单击左箭头,treetableview 会在后台选择子项父项(Item2),即使上下文菜单被选中并且在前台并且应该(imo)消耗事件,即使当它无法处理事件本身时。

有没有一种简单的方法来消耗所有导航事件,而上下文菜单是打开的,所以表格不会在后台对未使用的 KeyNavigations 做出反应?或者您对如何获得所需行为有任何其他建议?

演示该问题的小示例:

public class TreeTableViewContextMenuKeyBehaviourMain extends Application
{
  @Override
  public void start( final Stage primaryStage )
  {
    TreeItem<Item> root = new TreeItem<>( new Item( "Root" ) );

    TreeItem<Item> item1 = new TreeItem<>( new Item( "Item1" ) );
    TreeItem<Item> item11 = new TreeItem<>( new Item( "Item11" ) );
    TreeItem<Item> item2 = new TreeItem<>( new Item( "Item2" ) );
    TreeItem<Item> item22 = new TreeItem<>( new Item( "Item22" ) );

    root.getChildren().add( item1 );
    item1.getChildren().add( item11 );
    root.getChildren().add( item2 );
    item2.getChildren().add( item22 );

    TreeTableColumn<Item, String> column = new TreeTableColumn<>( "Column" );

    column.setCellValueFactory( new TreeItemPropertyValueFactory<Item, String>( "name" ) );

    final TreeTableView<Item> treeTableView = new TreeTableView<>( root );
    treeTableView.getColumns().add( column );
    treeTableView.setShowRoot( false );
    treeTableView.setColumnResizePolicy( TreeTableView.CONSTRAINED_RESIZE_POLICY );

    treeTableView.setRowFactory( new Callback<TreeTableView<Item>, TreeTableRow<Item>>()
    {
      @Override
      public TreeTableRow<Item> call( final TreeTableView<Item> tableView )
      {
        final TreeTableRow<Item> row = new TreeTableRow<>();
        final ContextMenu rowMenu = new ContextMenu();

        rowMenu.setOnShowing( ( event ) ->
        {
          System.out.println( "OPEN!" );

          //Clear and Rebuild Contextmenu dynamically.
            rowMenu.getItems().clear();
            Menu deleteMenu = new Menu( "Delete" );
            final MenuItem deleteItem = new MenuItem( row.getItem().getName() );
            deleteMenu.getItems().add( deleteItem );
            rowMenu.getItems().add( deleteMenu );
          } );

        //If the contextmenu has no childs setOnShowing will not be called, so we need a placeholder for dynamic context menus.
        final Menu placeholder = new Menu( "Delete" );
        rowMenu.getItems().add( placeholder );

        // only display context menu for non-null items:
        row.contextMenuProperty().bind(
            Bindings.when( Bindings.isNotNull( row.itemProperty() ) ).then( rowMenu )
                .otherwise( (ContextMenu) null ) );

        return row;
      }
    } );

    BorderPane layout = new BorderPane();
    layout.setCenter( treeTableView );
    Scene scene = new Scene( layout, 400, 400 );
    scene.getStylesheets().add( getClass().getResource( "application.css" ).toExternalForm() );
    primaryStage.setScene( scene );
    primaryStage.show();
  }

  public static void main( final String[] args )
  {
    launch( args );
  }
}

public class Item
{
  private final StringProperty name = new SimpleStringProperty();

  public Item( final String name )
  {
    this.name.set( name );
  }

  public String getName()
  {
    return name.get();
  }

  public void setName( final String name )
  {
    this.name.set( name );
  }

  public StringProperty nameProperty()
  {
    return name;
  }
}
4

1 回答 1

1

EventHandler您可以向您添加一个以ContextMenu使用KeyEvents试图汇集备份场景图的内容。

rowMenu.addEventHandler(KeyEvent.ANY, (KeyEvent t) ->
{
    t.consume();
});
于 2015-03-31T13:30:32.233 回答