How can I set the selection on a swt table using setSelection and setting reveal to false?

codegurl picture codegurl · Jan 14, 2011 · Viewed 10k times · Source

I'm trying to select some items in my table, but I DON'T want them to be revealed. The problem is that calling the method (seen below) automatically results in showSelected() being called inside of Table.class and that is not what I want.

tableViewer.getTable().setSelection(lastSelectedIndices);

I've tried setting the selection using tableViewer, but for some reason it doesn't work ex: tableViewer.setSelection(new StructuredSelection (lastSelectedIndices), false); this line of code does not cause anything to be selected.

Is there anyway I can go about selecting the table rows and NOT cause them to be revealed? In my system, I need to call setSelection every time after the grid refreshes so that the user doesn't lose his selected item. The problem occurs when the user scrolls down the grid --> if a refresh occurs at that point, the grid jumps back up to where the selected items are. It looks really weird when the user scrolls down a table and suddenly the scroll bar jumps up to the top.

Any help is greatly appreciated!

--code for setting up the table --

     // add table viewer
    tableViewer = new TableViewer(this, SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
    tableViewer.setUseHashlookup(true);
    tableViewer.addFilter(new GridPanelViewerFilter());
    tableViewer.setComparator(new GridPanelViewerComparator());
    tableViewer.setComparer(new GridPanelElementComparer());

    table = tableViewer.getTable();
    GridData tableGd = new GridData(SWT.FILL, SWT.FILL, true, true);
    table.setLayoutData(tableGd);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    // set table font
    setTableFont();

    // listen to paint events for anti aliasing 
    table.addPaintListener( ...etcetc

---code for refreshing the table --

        protected void refreshGrid() {
    if(!updateGrid) return;
    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {
            try {
                // check if disposed
                if (isDisposed()) {
                    log.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, getClass().getName() + ": " + "Grid already disposed"));
                    return;
                }
                // refresh table
                table.setRedraw(false);
                try {                   
                    // get last selected indices from mouse down event
                    tableViewer.refresh(true, false);
                    if(lastSelectedIndices != null){
                        tableViewer.getTable().deselectAll();
                        tableViewer.getTable().select(lastSelectedIndices);
                    }

                } catch (Exception e) {
                    log.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                            getClass().getName() + ": " + "Error at refresh table", e));
                }
                table.setRedraw(true);
                // process post grid refresh
                postGridRefresh();

            } catch (Exception e) {
                log.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, getClass().getName() + ": " + "Error during refresh grid", e));
            }
        }
    });
}

Answer

the.duckman picture the.duckman · Jan 14, 2011

Try using Table.select(indexes) to prevent revealing.

But using TableViewer.refresh() should actually preserve the selection. Just a wild guess, but maybe you try TableViewer.setUseHashlookup(true) before setting any input on your TableViewer.

Do you use a VIRTUAL Table? If so, try it first without the VIRTUAL flag.