【问题标题】:Why does Table.getItem(Point) always return the item from column zero?为什么 Table.getItem(Point) 总是从零列返回项目?
【发布时间】:2010-09-26 14:45:21
【问题描述】:

我已经实现了一个类,以便为 org.eclipse.swt.widgets.Table 类提供更灵活的提示。相信大家都知道,默认情况下,您会为整个表格获得一个工具提示 - 而不是每个单元格的单独提示。

我的对象创建了许多侦听器来实现它自己的位置敏感工具提示,但是,当我调用 Table.getItem(Point) 来获取鼠标悬停在其上的 TableItem(或单元格)时,它总是从列中返回单元格0 正确的行。

有问题的表是由 TableViewer 用这个咒语创建的......

viewer = new TableViewer( parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION );

我哪里出错了?

【问题讨论】:

    标签: swt eclipse-rcp jface


    【解决方案1】:

    答案,归根结底是TableItem代表一行,而不是一行中的一个单元格。这个小方法将为您获取列号...

    /**
     * Get the column from the given TableItem and Point objects 
     * @param pt
     * @param item
     * @return -1 if no column is found
     */
    public static int getColumn( Point pt, TableItem item )
    {
        int columns = item.getParent().getColumnCount();
        for (int i=0; i<columns; i++)
        {
            Rectangle rect = item.getBounds (i);
            if ( pt.x >= rect.x && pt.x < rect.x + rect.width ) return i;
        }
        return -1;
    }
    

    注意:如果您将鼠标完全悬停在表格边缘上方,则 Rectangle.intersects(Point) 可能会失败,因此我使用了简单的 X 比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      相关资源
      最近更新 更多