capint
10/29/2015 - 11:12 AM

Eclipse >> JFace >> Table Viewer

Eclipse >> JFace >> Table Viewer

// create a column for the first name
TableViewerColumn colFirstName = new TableViewerColumn(viewer, SWT.NONE);
colFirstName.getColumn().setWidth(200);
colFirstName.getColumn().setText("Firstname");
// The setLabelProvider() method on the TableViewerColumn expects an instance of the abstract CellLabelProvider class
// A default implementation of this class is provided by the ColumnLabelProvider class
colFirstName.setLabelProvider(new ColumnLabelProvider() {
  @Override
  public String getText(Object element) {
    Person p = (Person) element;
    return p.getFirstName();
  }
});

// create column for married property of Person
// uses getImage() method instead of getText()
// CHECKED and UNCHECK are fields of type Image

TableViewerColumn colMarried = new TableViewerColumn(viewer, SWT.NONE);
colMarried.getColumn().setWidth(200);
colMarried.getColumn().setText("Married");
colMarried.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
  return null;  // no string representation, we only want to display the image
}

@Override
public Image getImage(Object element) {
  if (((Person) element).isMarried()) {
    return CHECKED;
  } 
  return UNCHECKED;
  }
}); 
// You can implement your own content provider for a table by implementing the interface IStructuredContentProvider
// Eclipse provides an implementation of this interface via the ArrayContentProvider class
// set the content provider
viewer.setContentProvider(ArrayContentProvider.getInstance());

// provide the input to the viewer
// setInput() calls getElements() on the 
// content provider instance
viewer.setInput(someData...); 
/* Source: 
  http://www.vogella.com/tutorials/EclipseJFaceTable/article.html
  http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html
  http://www.eclipse.org/articles/Article-Table-viewer/table_viewer.html
*/
// define the TableViewer
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
      | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);

// create the columns 
// not yet implemented
createColumns(viewer);

// make lines and header visible
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);