星云网格 - 选择单个单元格(CellSelectionEnabled)

时间:2013-09-25 10:03:10

标签: java swt nebula

我正在使用Eclipse.org的Nebula Grid,并希望访问单个单元格。不是一个单独的GridItem,它可以通过grid.select(...)而不是一个单元来完成。所以我想说我有这样一个网格:

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);
}

就像我说的,grid.select选择整行,这不是我想要的。我也尝试过grid.selectCell(...),但由于某些原因,它也无法工作。使用的坐标具有很高的可能性:

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);
    }
});

任何想法?

1 个答案:

答案 0 :(得分:0)

对于网格,Point坐标表示相交列和行项。即,x坐标表示列的索引,y co-ord表示行项索引。

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);
}
});