SWT - Table vs. TableViewer

jkteater picture jkteater · Sep 14, 2012 · Viewed 7.1k times · Source

I am creating a new project using SWT. I will have 3 or 4 different tables in the project. I am fairly new to SWT and I find myself asking should I be using just the Table or should it be a TableViewer.

I am wanting to learn some good guidelines on when to use just the Table and when a TableViewer is the best route.

  1. What is the benefit of using a TableViewer instead of a Table?
  2. Should all the tables have a TableViewer?
  3. If I am working with data from the table, is just the Table the best way?

Just really wanting some clarity so as I create the project I do it the right way.

EDIT

I have created a Tablemodel class that I am using for my first table. But the createColumns method is specialized for that specific table.

Is it possible to have a template TableViewer class?
Can I change the method to be more usable for different tables?

Here is a snippet of the method:

private void createColumns() {

  String[] titles = { "ItemId", "RevId", "PRL", "Dataset Name", "EC Markup" };
  int[] bounds = { 150, 150, 100, 150, 100 };

  TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
  col.setLabelProvider(new ColumnLabelProvider() {
     public String getText(Object element) {
        if(element instanceof AplotDataModel.AplotDatasetData)
           return ((AplotDataModel.AplotDatasetData)element).getDataset().toString();
        return super.getText(element); 
        }
     }); 

  col = createTableViewerColumn(titles[1], bounds[1], 1);
  col.setLabelProvider(new ColumnLabelProvider() {
     public String getText(Object element) {
        if(element instanceof AplotDataModel.AplotDatasetData)
           return ((AplotDataModel.AplotDatasetData)element).getRev().toString();
        return super.getText(element); 
        }
     });

Answer

Baz picture Baz · Sep 15, 2012

In general, I would suggest a TableViewer. The viewer will take care of most things you would have to do yourself with a Table. Deleting and adding and moving items is easier as well as customizing how the items are displayed. Handling click events is really easy with a viewer.

There are few cases, where I would use a Table without a TableViewer. For example: When the table is only used to display a static set of items that never changes. In this case a TableViewer might be a little over the top.

However, you should keep in mind, that your project could grow and you might need those "simple" tables to do more than just display static items. In this case you would have to replace the table with a viewer which will be a lot of work.

So think twice before using a Table without a TableViewer.