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.
我已经实现了一个类来为org.eclipse.swt.widgets.Table类提供更灵活的tooptip。我相信大家都知道,默认情况下,您可以获得整个表格的一个工具提示 - 而不是每个单元格的单独提示。
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.
我的对象创建了许多侦听器来实现它自己的位置敏感工具提示,但是,当我调用Table.getItem(Point)来获取鼠标悬停在其上的TableItem(或单元格)时,它总是从第0列返回单元格。正确的行。
The table in question is created by a TableViewer with this incantation ...
有问题的表是由TableViewer用这个咒语创建的......
viewer = new TableViewer( parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION );
Wherever am I going wrong?
哪里出错了?
1 个解决方案
#1
3
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 ...
答案最后是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;
}
NB: Rectangle.intersects( Point ) can fail if you hover exactly over the table edges, so I've used a simple X comparison instead.
注意:如果你将鼠标悬停在表边缘上,Rectangle.intersects(Point)可能会失败,所以我使用了简单的X比较。
#1
3
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 ...
答案最后是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;
}
NB: Rectangle.intersects( Point ) can fail if you hover exactly over the table edges, so I've used a simple X comparison instead.
注意:如果你将鼠标悬停在表边缘上,Rectangle.intersects(Point)可能会失败,所以我使用了简单的X比较。