为什么Table.getItem(Point)总是从零列返回项目? [英] Why does Table.getItem(Point) always return the item from column zero?
问题描述
我实现了一个类,使我可以更灵活地对org.eclipse.swt.widgets.Table类进行优化。我相信大家都知道,默认情况下,您会获得整个表的一个工具提示,而不是每个单元格的单个提示。
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.
我的对象创建了许多侦听器来实现它自己的位置敏感工具提示,但是,当我调用Table.getItem(Point)以获取鼠标悬停在其上的TableItem(或单元格)时,它总是从正确行的第0列返回单元格。
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.
有问题的表格是由TableViewer创建的...
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 );
我哪里出错了?
推荐答案
最后,答案是TableItem代表一行,而不是一行中的单元格。这个小方法将为您获取列号...
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;
}
注意:如果将鼠标悬停在Rectangle.intersects(Point)上,可能会失败表边,所以我使用了一个简单的X比较。
NB: Rectangle.intersects( Point ) can fail if you hover exactly over the table edges, so I've used a simple X comparison instead.
这篇关于为什么Table.getItem(Point)总是从零列返回项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!