Why does Table.getItem (Point) always return an item from a null column?

I implemented the 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 entire table, rather than separate tooltips for each cell.

My object creates multiple listeners to implement its own location-sensitive tooltip, however, when I call Table.getItem (Point) to get the TableItem (or cell) that the mouse cursor hovers over, it always returns the cell from column 0 of the correct row.

The corresponding table is created by the TableViewer with this spell ...

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

      

Where would I go wrong?

0


source to share


1 answer


In the end, the answer is that the TableItem represents a row, not a cell within a row. This little method will get 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 to the edges of the table, so I used a simple X comparison instead.

+3


source







All Articles