//含有tableviewer的composite
public class ReaderTableViewComposite extends Composite {
public ReaderTableViewComposite(Composite parent) {
super(parent,SWT.NONE);
populateControls(parent);
}
private void populateControls(Composite parent) {
parent.setLayout(new FillLayout());
TableViewer viewer = new TableViewer(parent,
SWT.CHECK|SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setLayout(new TableLayout());
TableColumn id = new TableColumn(table,SWT.NONE);
id.setText("ID");
id.pack();
TableColumn name = new TableColumn(table,SWT.NONE);
name.setText("姓名");
name.pack();
TableColumn sex = new TableColumn(table,SWT.NONE);
sex.setText("性别");
sex.pack();
TableColumn level = new TableColumn(table,SWT.NONE);
level.setText("级别");
level.pack();
viewer.setContentProvider(new IStructuredContentProvider(){
@Override
public void dispose() {}
@Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {}
@Override
public Object[] getElements(Object arg0) {
return ((java.util.List)arg0).toArray();
}
});
//viewer.setInput(new UserDaoXMLImpl().findAll());
}
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(500,375);
shell.setText("swt");
ReaderTableViewComposite c = new ReaderTableViewComposite(shell);
shell.open();
while(!shell.isDisposed()){
while(!display.readAndDispatch()){
display.sleep();
}
}
shell.dispose();
}
}
//测试类
public class Test extends ApplicationWindow {
public Test() {
super(null);
}
@Override
protected Control createContents(Composite parent) {
Composite c = new ReaderTableViewComposite(parent);
parent.pack();
return parent;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test cv = new Test();
cv.setBlockOnOpen(true);
cv.open();
Display.getCurrent().dispose();
}
}自己解决了,看能给自己分不