我正在使用Nebula Grid在我的RCP应用程序中显示Excel工作表。我需要选择所有行。我的代码如下:
private void addKeyListener() {
this.gv.getGrid().addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent e) {
if(e.stateMask==SWT.CTRL && e.keyCode =='a'){
ArrayList al = (ArrayList) gv.getInput();
//System.out.println("ctrl+c pressed");
gv.setSelection(new StructuredSelection(al.toArray()),true);
//gv.getGrid().setSelection(0, al.size()-1);
//gv.getGrid().selectAll();
//gv.getGrid().setSelection(new int[]{1,2});
//gv.getGrid().setSelection(1);
//gv.getGrid().setSelectionEnabled(true);
//gv.getGrid().select(new int[]{1,2,3});
//gv.refresh();
}
但这不起作用。我错过了什么?
答案 0 :(得分:2)
您还没有提到代码中网格使用的选择行为类型。即行或单元格选择,单个或多个选择(SWT.SINGLE& SWT.MULTI样式)。
在初始化Grid时将样式设置为SWT.MULTI似乎可以解决问题。
以下是适用于我的代码段。
public class GridSnippet {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
final Grid grid = new Grid(shell,SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
grid.setHeaderVisible(true);
// grid.setCellSelectionEnabled(true);
GridColumn column = new GridColumn(grid,SWT.NONE);
column.setText("Column 1");
column.setWidth(100);
GridItem item1 = new GridItem(grid,SWT.NONE);
item1.setText("First Item");
GridItem item2 = new GridItem(grid,SWT.NONE);
item2.setText("Second item");
GridItem item3 = new GridItem(grid,SWT.NONE);
item3.setText("Third Item");
grid.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent e) {
if(e.stateMask==SWT.CTRL && e.keyCode =='a'){
System.out.println("ctrl+c pressed");
grid.selectAll();
}
}
});
shell.setSize(200,200);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}