Search code examples
javaswtnebula

Nebula Grid - Select individual cells (CellSelectionEnabled)


I am using Eclipse.org's Nebula Grid and want to access an individual cell. Not an individual GridItem, which can be accomplished by grid.select(...) but a cell. So lets say i have a grid like this:

final Grid grid = new Grid(shell,SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
grid.setCellSelectionEnabled(true);
grid.setHeaderVisible(true);

GridColumn column = new GridColumn(grid, SWT.None);
column.setWidth(80);
GridColumn column2 = new GridColumn(grid, SWT.None);
column2.setWidth(80);
for(int i = 0; i<50; i++)
{
    GridItem item = new GridItem(grid, SWT.None);
    item.setText("Item" + i);
}

Like i said, grid.select selects the whole row, which is not what i want. I also tried grid.selectCell(...), but for some reason that wont work either. The coordinates used are with a high likeliness correct:

Button btn = new Button(shell, SWT.PUSH);
btn.setText("test");
btn.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
    Point pt = new Point(400,300);
    grid.selectCell(pt);
    }
});

Any Ideas?


Solution

  • For a Grid, the Point co-ordinates represent the intersecting column and the row item. i.e, x co-ordinate represents the index of the column and y co-ord is the row item index.

    Button btn = new Button (shell, SWT.PUSH);
    btn.setText ("test");
    btn.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
    
           // Here the x co-ordinate of the Point represents the column
           // index and y co-ordinate stands for the row index.
           // i.e, x = indexOf(focusColumn); and y = indexOf(focusItem);
           Point focusCell = grid.getFocusCell();
           grid.selectCell(focusCell);
    
            // eg., selects the intersecting cell of the first column(index = 0)
            // in the second row item(rowindex = 1).
            Point pt = new Point(0, 1);
            grid.selectCell(pt);
    }
    });