1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
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.Table;
import org.eclipse.swt.widgets.TableColumn;
public class Snippet38 {
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite composite = new Composite(shell, SWT.BORDER);
GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
layoutData.heightHint = 400;
composite.setLayoutData(layoutData);
TableColumnLayout tableLayout = new TableColumnLayout();
composite.setLayout(tableLayout);
Table table = new Table(composite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
TableViewer viewer = new TableViewer(table);
table.setLinesVisible(true);
table.setHeaderVisible(true);
String[] titles = { "Description", "Location" };
int count = 128;
List<Item> items = new ArrayList<Item>();
Item obj;
for (int i = 0; i < count; i++)
{
obj = new Item("Descr " + (count - i), "Location " + i);
items.add(obj);
}
TableViewerColumn descrColumn = new TableViewerColumn(viewer, SWT.LEFT);
descrColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element)
{
String text = "";
if (element instanceof Item)
{
text = ((Item) element).getDescription();
}
return text;
}
});
TableColumn descrColumnControl = descrColumn.getColumn();
descrColumnControl.setText(titles[0]);
tableLayout.setColumnData(descrColumnControl, new ColumnWeightData(2));
TableViewerColumn locationColumn = new TableViewerColumn(viewer, SWT.LEFT);
locationColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element)
{
String text = "";
if (element instanceof Item)
{
text = ((Item) element).getLocation();
}
return text;
}
});
TableColumn locationColumnControl = locationColumn.getColumn();
locationColumnControl.setText(titles[1]);
tableLayout.setColumnData(locationColumnControl, new ColumnWeightData(1));
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(items);
shell.setSize(600, 500);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* <p>
* POJO to be displayed.
* </p>
*/
static class Item {
private String description;
private String location;
public Item(String pDescription, String pLocation) {
description = pDescription;
location = pLocation;
}
/**
* @return the description.
*/
public String getDescription()
{
return description;
}
/**
* @return the location.
*/
public String getLocation()
{
return location;
}
}
} |
Partager