加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:23:17 所属栏目:Java 来源:网络整理
导读:我正在尝试在我的桌子中选择一些项目,但我不希望它们被揭示. 问题是调用方法(如下所示)会自动导致在Table.class中调用showSelected(),这不是我想要的. tableViewer.getTable().setSelection(lastSelectedIndices); 我已经尝试使用tableViewer设置选择,但由于
我正在尝试在我的桌子中选择一些项目,但我不希望它们被揭示.
问题是调用方法(如下所示)会自动导致在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标志.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读