我试过这个:
import java.util.HashSet;
import java.util.Set;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class CorrelationDlg extends Dialog {
private final GridLayout composite;
private Spinner spinner;
private Button button;
private final Table ancestors;
private final Table descendants;
public CorrelationDlg(Shell shell) {
super(shell);
composite = new GridLayout(4, false);
shell.setLayout(composite);
spinner = new Spinner(shell, SWT.FILL);
spinner.setMinimum(0);
spinner.setMaximum(7);
spinner.setIncrement(1);
GridData spinnerData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
spinnerData.horizontalSpan = 3;
spinner.setLayoutData(spinnerData);
spinner.pack();
button = new Button(shell, SWT.CHECK);
button.setText("Self:");
GridData buttonData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
buttonData.horizontalSpan = 3;
button.setLayoutData(buttonData);
button.pack();
ancestors = new Table(shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
final TableColumn ancestorsColumn = new TableColumn(ancestors, SWT.NONE);
ancestorsColumn .setText("Ancestors");
ancestors.setHeaderVisible(true);
GridData ancestorsData = new GridData(GridData.FILL_BOTH, SWT.BEGINNING, true, false);
ancestorsData.horizontalSpan = 3;
ancestors.setLayoutData(ancestorsData);
descendants = new Table(shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
final TableColumn descendantsColumn = new TableColumn(descendants, SWT.NONE);
descendantsColumn.setText("Descendants");
descendants.setHeaderVisible(true);
GridData descendantsData = new GridData(GridData.FILL_BOTH, SWT.BEGINNING, true, false);
descendantsData.horizontalSpan = 1;
descendants.setLayoutData(descendantsData);
}
public void addAncestors(Set<String> resourceKinds) {
for(final String resourceKind: resourceKinds) {
final TableItem item = new TableItem(ancestors, SWT.NONE);
item.setText(resourceKind);
}
ancestors.pack();
}
public void addDescendants(Set<String> resourceKinds) {
for(final String resourceKind: resourceKinds) {
final TableItem item = new TableItem(descendants, SWT.NONE);
item.setText(resourceKind);
}
descendants.pack();
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
CorrelationDlg dlg = new CorrelationDlg(shell);
Set<String> ancestors = new HashSet<>();
ancestors.add("A1");
ancestors.add("A2");
dlg.addAncestors(ancestors);
Set<String> ds = new HashSet<>();
ds.add("A1");
ds.add("A2");
dlg.addDescendants(ds);
shell.setSize(200, 200);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
但目前我无法查看表格项目。 你能指出这有什么问题吗?
答案 0 :(得分:1)
您没有设置列宽并且没有正确pack()
它们,因此它们的宽度为0.
您可以使用setWidth
设置宽度,例如:
ancestorsColumn.setWidth(100);
或&#34; pack&#34;他们使用pack()
方法,例如在addAncestor
中,您可以添加以下内容:
ancestors.getColumn(0).pack();
答案 1 :(得分:1)
您的TableItem
肯定会被添加,如果您调整列的大小,可以看到:
您需要做的是通过TableColumn#setWidth(int)
或通过TableLayout
指定列的宽度(如果您的表格将调整大小,也可能指定TableColumnLayout
,因此布局将保持文档的比例)。