java – 如何使用setSelection在swt表上设置选择并将reveal设置为false?

我正在尝试在我的桌子中选择一些项目,但我不希望它们被揭示.
问题是调用方法(如下所示)会自动导致在Table.class中调用showSelected(),这不是我想要的.

tableViewer.getTable().setSelection(lastSelectedindices);

我已经尝试使用tableViewer设置选择,但由于某种原因它不起作用
例如:tableViewer.setSelection(new StructuredSelection(lastSelectedindices),false);
这行代码不会导致任何选择.

无论如何我可以选择表格行而不是让它们被揭示?
在我的系统中,每次网格刷新后我都需要调用setSelection,这样用户不会丢失他选择的项目.当用户向下滚动网格时出现问题 – >如果在该点发生刷新,则网格会跳回到所选项目所在的位置.当用户向下滚动一个表并突然滚动条跳到顶部时,它看起来很奇怪.

任何帮助是极大的赞赏!

– 用于设置表格的代码

// 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

—刷新表的代码

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,getClass().getName() + ": " + "Error at refresh table",e));
                }
                table.setRedraw(true);
                // process post grid refresh
                postGridRefresh();

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

解决方法

尝试使用Table.select(索引)来防止泄露.

但是使用TableViewer.refresh()实际上应该保留选择.只是一个疯狂的猜测,但也许你在TableViewer上设置任何输入之前尝试TableViewer.setUseHashlookup(true).

你使用VIRTUAL表吗?如果是这样,请先尝试不使用VIRTUAL标志.

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...