javamultiple-columnsnebula

How to select columns in Nebula GridTable


I'm currently working with Nebula GridTable and would like to implement a copy&paste functunality for rows and columns of a table. The selection of a row was working out of the box, but I would like to have a column selected, if I push the header cell. The GridTableViewer is created as follows

new GridTableViewer(parent, SWT.FULL_SELECTION | SWT.V_SCROLL | 
SWT.H_SCROLL | SWT.MULTI |SWT.WRAP | SWT.VIRTUAL)

To do so, I implemented a SelectionListener for the grid as follows:

v.getGrid().addSelectionListener(new SelectionListener(){

        @Override
        public void widgetSelected(SelectionEvent e) 
        {
            v.getGrid().selectColumn(e.y);
            v.getGrid().update();

        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            // TODO Auto-generated method stub

        }

      });

The debugger lets me suggest, that the column is selected, but it isn't highlighted in the UI. What do I have to do to get the multi column selection and highlighting working?

Best regards, Christoph


Solution

  • I found it out myself and I was very tricky to solve. The configuration of the GridViewer was correct, but it is importent to the cell selection flag on the grid. So I added the following line to enable cell selection

    tableViewer.getGrid().setCellSelectionEnabled(true);
    

    To select a column by clicking on the table header can be implemented as follows.

    column.addSelectionListener(new SelectionListener(){
    
            @Override
            public void widgetSelected(SelectionEvent e) 
            {
                int column = ((GridColumn) e.item).getCellRenderer().getColumn();
                tableViewer.getGrid().selectColumn(column);
            }
    
            @Override
            public void widgetDefaultSelected(SelectionEvent e) { }
    
        });