开发者

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

I'm trying to select some items in my table, but I DON'T want them to 开发者_StackOverflow中文版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));
            }
        }
    });
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜