Search code examples
javaswt

SWT table: How to retrieve row number under mouse cursor?


I'd like to retrieve a row number under the mouse cursor (as I need to display some relevant information in tooltip based on the mouse position).

I already found a way to compute the column number based on the cursor position (Why does Table.getItem(Point) always return the item from column zero?), however can't find out the row number.

final Table table = ...

table.addListener(SWT.MouseHover, new Listener() {
    @Override
    public void handleEvent(Event event) {      
        final Point point = new Point(event.x, event.y);
        final TableItem item = table.getItem(point);
        for (int i = 0; i < COLUMN_CNT; i++) {
            final Rectangle rect = item.getBounds(i);
            if (rect.contains(point)) {
                // now I'm in the right column
                ...         
            }
        }
    }
}

What would be a way to achieve that?


Solution

  • Use the Table indexOf method:

    Table table = ... your table
    
    TableItem item = ... your item
    
    int row = table.indexOf(item);