为什么 Table.getItem(Point) 总是从零列返回项目?

发布于 2024-07-10 00:42:45 字数 406 浏览 4 评论 0原文

我实现了一个类,为 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 );

我哪里出错了?

I've implemented a class to give me a more flexible tooptip for the org.eclipse.swt.widgets.Table class. As I'm sure you all know, by default you get one tooltip for the whole table - and not individual tips for each cell.

My object creates a number of listeners to implement it's own location sensitive tooltip, however, when I call Table.getItem(Point) to get the TableItem (or cell) over which the mouse is hovering it always returns the cell from column 0 of the correct row.

The table in question is created by a TableViewer with this incantation ...

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

Wherever am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

驱逐舰岛风号 2024-07-17 00:42:45

最终的答案是 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 比较。

The answer, in the end is that TableItem represents a row, and not a cell within a row. This little method will fetch the column number for you ...

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

NB: Rectangle.intersects( Point ) can fail if you hover exactly over the table edges, so I've used a simple X comparison instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文