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

标签 java swt jface tableviewer

我正尝试在我的表格中选择一些项目,但我不希望它们被显示出来。 问题是调用该方法(见下文)会自动导致 showSelected()Table.class 内部被调用,这不是我想要的。

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, 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));
            }
        }
    });
}

最佳答案

尝试使用 Table.select(indexes) 来防止泄露。

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

您使用虚拟表吗?如果是这样,请先尝试不使用 VIRTUAL 标志。

关于java - 如何使用 setSelection 并将 reveal 设置为 false 在 swt 表上设置选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4689985/

相关文章:

java - 如何在Android中获取Spinner Selected Item

java - org.eclipse.swt.widgets.Composite 上的字体在 Linux 上乱七八糟

java - 是否有用于选择目录和文件的 SWT 或 JFace 对话框?

java - 使用 FillLayout 在 Composite 上记录 SWT MouseEnter/Exit 事件

java - 在 TreeViewer 中隐藏菜单项

java - eclipse RCP、SWT、JFace : How to create a dialog that is modal only to a view (not the entire shell)?

java - 如何检查类是否有变量并在其他类中声明它

java - 重用复杂的 spring-fox swagger 注释

javascript - 网页性能测试

java - SWT 事件从 shell 委托(delegate)到 SWT.Browser