Search code examples
eclipsejfacenebula

Nebula GridViewer Selection not working


I am using Nebula Grid to display an excel sheet in my RCP application. I have an requirement to select all rows. My code is as follows:

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

but this is not working. what am I missing ?


Solution

  • You have not mentioned what type of selection behaviour is being used by the grid in your code. i.e, Row or Cell Selection, single or multiple selections (SWT.SINGLE & SWT.MULTI styles).

    Setting the style to SWT.MULTI when initializing the Grid seems to do the trick.

    Here is the snippet that works for me.

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