如何在上下文菜单打开时禁止TreeTableView的键导航事件?

时间:2015-03-31 10:17:54

标签: javafx contextmenu keyevent treetableview

当我将一个上下文菜单添加到treetableview时,它仍然不幸地仍然对上下文菜单没有使用的KeyEvent做出反应。

enter image description here

例如,点击子项目(Item22)打开其上下文菜单然后点击左箭头treetableview选择背景中的子项父项(Item2),尽管选择了上下文菜单并且在前景中并且应该(imo)使用事件,即使它无法处理事件本身。

是否有一种简单的方法来使用所有导航事件,而contextmenu是打开的,所以表格在后台未对未使用的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;
  }
}

1 个答案:

答案 0 :(得分:1)

您可以向EventHandler添加ContextMenu以使用试图漏斗备份场景图的KeyEvents消息。

rowMenu.addEventHandler(KeyEvent.ANY, (KeyEvent t) ->
{
    t.consume();
});