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