0
ScrolledComposite에있는 큰 테이블 뷰어가있는 대화 상자가 있습니다. TableViewer에서 항목을 선택하려면 ScrollledComposite를 프로그래밍 방식으로 스크롤해야합니다. 쉬운 작업처럼 보이지만 실제로 스택이 있습니다. 나는 많은 생각을했으며 그들 중 누구도 일하고 있지 않다.프로그래밍 방식으로 ScrolledComposite scroll TableViewer 항목
내 샘플 코드가있다 :
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
/**
* Scroll a Viewer 99th element
*
*/
public class Snippet008RevealElement {
public class MyModel {
public int counter;
public MyModel(int counter) {
this.counter = counter;
}
@Override
public String toString() {
return "Item " + this.counter;
}
}
public Snippet008RevealElement(Shell shell) {
ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
scrolledComposite.setLayoutData(data);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
Composite main = new Composite(scrolledComposite, SWT.NONE);
main.setLayout(new GridLayout());
main.setLayoutData(new GridData(GridData.FILL_BOTH));
final TableViewer v = new TableViewer(main);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(ArrayContentProvider.getInstance());
MyModel[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);
// v.reveal(model[99]);
// v.getTable().setSelection(99);
TableItem[] items = v.getTable().getItems();
TableItem item = items[99];
scrolledComposite.getVerticalBar().setSelection(item.getBounds().y);
scrolledComposite.setContent(main);
scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
private MyModel[] createModel() {
MyModel[] elements = new MyModel[100];
for(int i = 0; i < 100; i++) {
elements[i] = new MyModel(i);
}
return elements;
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet008RevealElement(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
실제 문제 또는 버그가 실제 TableItem 항목에 대한 경계 = 항목을 찾을 수 있습니다 [99] 합성물에는 보이지 않습니다.
scrolledComposite.setContent(main);
scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
// Need to run the calculations after all size calculations have been done
// So do asynchronously.
final Display display = scrolledComposite.getDisplay();
display.asyncExec(() ->
{
final Table table = v.getTable();
final TableItem item = table.getItem(99);
Rectangle itemBounds = item.getBounds();
// Convert to be relative to scrolled composite
itemBounds = display.map(viewer.getTable(), scrolledComposite, itemBounds);
scrolledComposite.setOrigin(0, itemBounds.y);
});
참고 :