本文介绍了SWT-表与TableViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SWT创建一个新项目.我在项目中将有3或4个不同的表.我对SWT相当陌生,我发现自己在问我应该只使用Table还是使用TableViewer.

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.

我想学习一些关于何时仅使用Table以及何时使用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. 使用TableViewer代替Table有什么好处?
  2. 所有表都应有一个TableViewer吗?
  3. 如果我正在处理表中的数据,那么Table是最好的方法吗?
  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.

编辑

我创建了一个用于第一个表的Tablemodel类.但是createColumns方法专用于该特定表.

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

是否可以有模板TableViewer类?
我可以更改该方法以使其更适用于不同的表吗?

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

以下是该方法的摘要:

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

推荐答案

通常,我建议使用TableViewer.观看者将使用Table来处理您必须要做的大多数事情.删除,添加和移动项目以及自定义项目的显示方式更加容易.使用查看器来处理点击事件真的很容易.

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.

在少数情况下,我会使用不带TableViewerTable.例如:当该表仅用于显示一组永不更改的静态项目时.在这种情况下,TableViewer可能会在顶部上方.

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.

因此,在使用不带TableViewerTable之前,请三思.

So think twice before using a Table without a TableViewer.

这篇关于SWT-表与TableViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 11:04