1

当用户点击特定键时,我想打开一个上下文菜单。所以我注册了一个 Key EventHandler,并成功打开了一个上下文菜单,但不幸的是我无法确定上下文菜单的正确位置。

我想在当前选定的单元格下方打开它:

在此处输入图像描述

我无法确定要显示的上下文菜单的正确位置:

//Showcase Code:
treeTableView.setOnKeyPressed( new EventHandler<KeyEvent>()
{
   @Override
   public void handle( final KeyEvent keyEvent )
   {
    final TreeItem<Item> selectedItem = treeTableView.getSelectionModel().getSelectedItem();

    if ( selectedItem != null )
    {
      if ( keyEvent.getCode().equals( KeyCode.INSERT ) )
      {
        final ContextMenu rowMenu = new ContextMenu();

        Menu deleteMenu = new Menu( "Delete" );
        final MenuItem deleteItem = new MenuItem( selectedItem.getValue().getName() );
        deleteMenu.getItems().add( deleteItem );
        rowMenu.getItems().add( deleteMenu );
        rowMenu.show( treeTableView.getScene().getWindow() );//How to get X and Y of current selected cell/row?
      }
    }
   }
 }

任何人都可以帮我从当前选定行/单元格的 treeTableViews 左下角确定正确的 X 和 Y 位置吗?

编辑:

添加了小例子:

public class TreeTableViewKeyListenerOnRow 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> item2 = new TreeItem<>( new Item( "Item2" ) );
    TreeItem<Item> item3 = new TreeItem<>( new Item( "Item3" ) );

    root.getChildren().add( item1 );
    item1.getChildren().add( item2 );
    root.getChildren().add( item3 );

    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<>();
        row.setFocusTraversable( true );

        row.setOnKeyPressed( new EventHandler<KeyEvent>()
        {
          @Override
          public void handle( final KeyEvent keyEvent )
          {
            System.out.println( "FIRE!" );//Not Fireing?

            final TreeItem<Item> selectedItem = row.getTreeItem();

            if ( selectedItem != null )
            {
              if ( keyEvent.getCode().equals( KeyCode.INSERT ) )
              {
                final ContextMenu menu = new ContextMenu();

                Menu deleteMenu = new Menu( "Delete" );
                final MenuItem deleteItem = new MenuItem( selectedItem.getValue().getName() );
                deleteMenu.getItems().add( deleteItem );
                menu.getItems().add( deleteMenu );

                // compute bounds of row in screen:
                Bounds boundsInScreen = row.localToScreen( row.getBoundsInLocal() );
                double x = boundsInScreen.getMinX();
                double y = boundsInScreen.getMinY();

                menu.show( treeTableView.getScene().getWindow(), x, y );
              }
            }
          }
        } );

        return row;
      }
    } );

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

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

1 回答 1

2

向各个TreeTableRows 注册上下文菜单。然后您可以调用row.getBoundsInLocal()以获取行的边界,并row.localToScreen(...)转换为屏幕坐标。

treeTableView.setRowFactory(ttv -> {
    TreeTableRow<Item> row = new TreeTableRow<>();
    row.setOnKeyPressed( new EventHandler<KeyEvent>() {
       @Override
       public void handle( final KeyEvent keyEvent ) {
           final TreeItem<Item> selectedItem = row.getTreeItem();

           if ( selectedItem != null ) {
               if ( keyEvent.getCode().equals( KeyCode.INSERT ) ) {
                   final ContextMenu rowMenu = new ContextMenu();

                   Menu deleteMenu = new Menu( "Delete" );
                   final MenuItem deleteItem = new MenuItem( selectedItem.getValue().getName() );
                   deleteMenu.getItems().add( deleteItem );
                   rowMenu.getItems().add( deleteMenu );

                   // compute bounds of row in screen:
                   Bounds boundsInScreen = row.localToScreen(row.getBoundsInLocal());
                   double x = boundsInScreen.getMinX();
                   double y = boundsInScreen.getMinY();

                   rowMenu.show( treeTableView.getScene().getWindow() );//How to get X and Y of current selected cell/row?
               }
           }
       }
   }    
   return row ;
});
于 2015-03-23T15:59:33.163 回答