4

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));
            }
        }
    });
}
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
codegurl
  • 131
  • 3
  • 5

2 Answers2

1

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.

the.duckman
  • 6,276
  • 3
  • 21
  • 21
  • I used TableViewer.refresh() and TableViewer.setUseHashlookup(true) but the selection does not remain preserved.. I tried the line you suggested, Table.select(indices) but it still results in the table scrolling to the selected indices. Interestingly, this method doesn't call showSelection() inside of Table.class (unlike setSelection). Do you think some other class / code is causing the selection to be revealed? – codegurl Jan 17 '11 at 02:43
0

This is just a hint, but maybe it works for you. Try using the setSelection() method like this:

tableViewer.getTable().setSelection(lastSelectedIndices, false);

The last parameter (boolean) should be set to true if the selection is to be made visible, and false otherwise.

f1v3
  • 127
  • 3
  • 8