【问题标题】:Nebula GridViewer Selection not working星云 GridViewer 选择不起作用
【发布时间】:2015-07-24 13:34:26
【问题描述】:

我正在使用 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();
            }

但这不起作用。我错过了什么?

【问题讨论】:

    标签: eclipse jface nebula


    【解决方案1】:

    您没有提到代码中的网格正在使用哪种类型的选择行为。即行或单元格选择、单选或多选(SWT.SINGLE 和 SWT.MULTI 样式)。

    在初始化 Grid 时将样式设置为 SWT.MULTI 似乎可以解决问题。

    这是适合我的 sn-p。

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

    【讨论】:

    • 我使用了以下样式位gv = new GridTableViewer(this.container, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);,但它不起作用。我将其更改为gv = new GridTableViewer(this.container, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);。选择全部现在正在工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 2020-05-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多