SWT - Table vs. TableViewer

2019-06-17 02:51发布

问题:

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

回答1:

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.



回答2:

http://sunnyshekhar.blogspot.com/2013/04/creating-swt-table-below-i-will-try-to.html

Here i have tried to explain how to create a table in swt . I suppose creating a table in swt is easier said than done . There are a lot of complexities we have to face . Implementation is a bit difficult to understand . But we have a lot of customization available in swt . Also one point to note would be that Swt is a layer above Os so in Swt we use what ever the Os provides us . Sometimes its difficult to create a check box and move it in the center of the cell . There are a lot of similar problems that i faced while creating the table but you always have hacks or workaround for them . In Swt we have well defined classes which if u implement gives a variety of methods to play around with the table .



回答3:

I would suggest you to use TableViewer always unless you want to do lot of customization.

http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html

Good article to start with

http://www.vogella.com/articles/EclipseJFaceTable/article.html#tutorial_jfacetableviewer_usage