如何获取当前所选单元格的当前位置?

时间:2015-03-23 15:39:55

标签: javafx position contextmenu keylistener treetableview

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

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

enter image description here

我迷失了确定上下文显示的正确位置:

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

1 个答案:

答案 0 :(得分:2)

使用单个TreeTableRow注册上下文菜单。然后,您可以调用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 ;
});