Nebula GridViewer 选择不起作用
Nebula GridViewer Selection not working
我正在使用 Nebula Grid 在我的 RCP 应用程序中显示 excel sheet。我需要 select 所有行。我的代码如下:
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();
}
但这不起作用。我错过了什么?
您没有在代码中提到网格使用的是哪种类型的选择行为。即,行或单元格选择,单选或多选(SWT.SINGLE & SWT.MULTI 样式)。
在初始化网格时将样式设置为 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 ();
}
}
我正在使用 Nebula Grid 在我的 RCP 应用程序中显示 excel sheet。我需要 select 所有行。我的代码如下:
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();
}
但这不起作用。我错过了什么?
您没有在代码中提到网格使用的是哪种类型的选择行为。即,行或单元格选择,单选或多选(SWT.SINGLE & SWT.MULTI 样式)。
在初始化网格时将样式设置为 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 ();
}
}