2013-09-25 44 views
0

我正在使用Eclipse.org的星雲網格並希望訪問單個單元格。不是一個單獨的GridItem,它可以通過grid.select(...)來完成,而是一個單元格。因此,可以說,我有一個這樣的網格: 星雲網格 - 選擇單個單元格(CellSelectionEnabled)

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

任何想法?

回答

0

對於網格,點座標表示相交的列和行項目。即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); 
} 
});