【问题标题】:How can I set the selection on a swt table using setSelection and setting reveal to false?如何使用 setSelection 在 swt 表上设置选择并将显示设置为 false?
【发布时间】:2018-04-04 15:27:34
【问题描述】:

我正在尝试选择表中的一些项目,但我不希望它们被泄露。 问题是调用该方法(见下文)会自动导致在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, 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));
            }
        }
    });
}

【问题讨论】:

    标签: java swt jface tableviewer


    【解决方案1】:

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

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

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

    【讨论】:

    • 我使用了 TableViewer.refresh() 和 TableViewer.setUseHashlookup(true) 但选择没有保留。我尝试了你建议的行 Table.select(indices) 但它仍然导致表格滚动到选定的索引。有趣的是,此方法不会在 Table.class 内部调用 showSelection()(与 setSelection 不同)。您是否认为其他一些类/代码导致选择被显示?
    【解决方案2】:

    这只是一个提示,但也许它对你有用。 尝试像这样使用setSelection() 方法:

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

    如果要使选择可见,则最后一个参数(布尔值)应设置为 true,否则为 false。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2020-11-23
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      相关资源
      最近更新 更多