Vardan Hovhannisyan Vardan Hovhannisyan - 1 year ago 54
Java Question

Can not see table items in SWT Table

I am trying to create the following dialog:
enter image description here

I have tried this:

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


But currently I am unable to see table items.
Could you please point out what is the problem with this?

Answer Source

You didn't set the columns width and didn't correctly packed them, so their width is 0.

You can set the width using setWidth, for example:

ancestorsColumn.setWidth(100);

or pack them with their pack method, for example in addAncestor you could add this:

ancestors.getColumn(0).pack();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download