NatTable中的数据不显示

Data in the NatTable is not displayed

对象已添加,但未显示在 table 中。 table 中这两个添加对象的位置已分配,但所有单元格都是空的。

有人可以推荐关于 NatTable 的好的文档,或者关于 NatTable 的写得很好的源代码吗?

    ArrayList<Student> students = new ArrayList<>();

    final ColumnGroupModel columnGroupModel = new ColumnGroupModel();
    ColumnHeaderLayer columnHeaderLayer;

    students.add(new Student("Andrey","1", new Exam("Math", 4)));
    students.add(new Student("John","2", new Exam("Physics", 5)));
    String[] propertyNames = { "Full Name", "Group", "Name", "Mark" };
    DefaultBodyDataProvider<Student> bodyDataProvider = new DefaultBodyDataProvider<Student>(students, propertyNames);
    ColumnGroupBodyLayerStack bodyLayer = new ColumnGroupBodyLayerStack(new DataLayer(bodyDataProvider), columnGroupModel);

    DefaultColumnHeaderDataProvider defaultColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames);
    DefaultColumnHeaderDataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(defaultColumnHeaderDataProvider);
    columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
    ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, bodyLayer.getSelectionLayer(), columnGroupModel);

    columnGroupHeaderLayer.addColumnsIndexesToGroup("Exams", 2, 3);
    columnGroupHeaderLayer.setGroupUnbreakable(2);

    final DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
    DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
    ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());

    final DefaultCornerDataProvider cornerDataProvider = new DefaultCornerDataProvider(defaultColumnHeaderDataProvider, rowHeaderDataProvider);
    DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
    ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnGroupHeaderLayer);

    GridLayer gridLayer = new GridLayer(
            bodyLayer,
            columnGroupHeaderLayer,
            rowHeaderLayer,
            cornerLayer);

    NatTable table = new NatTable(shell, gridLayer, false);
    table.configure();

Class 学生和考试

class Student {
   String name;
   String groupNumber;
   Exam exam = new Exam();

   public Student() { }

   public Student(String name, String groupNumber, Exam exam) {
    super();
    this.name = name;
    this.groupNumber = groupNumber;
    this.exam = exam;
   }
}

class Exam {
  String name;
  int mark;

  public Exam() { }

  public Exam(String name, int mark) {
    super();
    this.name = name;
    this.mark = mark;
  }
}

您在没有默认配置的情况下创建了 NatTable 实例。因此没有配置画家

在这里查看我们的入门教程:http://www.vogella.com/tutorials/NatTable/article.html

  1. 替代

    NatTable table = new NatTable(shell, gridLayer, false);
    table.configure();
    

    NatTable table = new NatTable(shell, gridLayer, true);
    

    所以 table 自动配置自己,否则你必须自己指定配置。

  2. DefaultBodyDataProvider使用反射从指定的class获取数据,所以Student一定是一个合适的beanclass,所以它有是 public 并且必须具有其属性的吸气剂。如果您还想检索 Exam 属性,您可以在 'Student':

    中为它们创建 getter
    public class Student {
        String name;
        String groupNumber;
        Exam exam = new Exam();
    
        public Student() {
        }
    
        public Student(String name, String groupNumber, Exam exam) {
            super();
            this.name = name;
            this.groupNumber = groupNumber;
            this.exam = exam;
        }
    
        public String getName() {
            return name;
        }
    
        public String getGroupNumber() {
            return groupNumber;
        }
    
        public Exam getExam() {
            return exam;
        }
    
        public String getExamName() {
            return exam.getName();
        }
    
        public int getExamMark() {
            return exam.getMark();
        }
    
    }
    
    public class Exam {
        String name;
        int mark;
    
        public Exam() {
        }
    
        public Exam(String name, int mark) {
            super();
            this.name = name;
            this.mark = mark;
        }
    
        public String getName() {
            return name;
        }
    
        public int getMark() {
            return mark;
        }
    }
    
  3. propertyNames 应该包含 Student 中变量的实际名称,因为它们用于通过反射检索 getter,例如:

    String[] propertyNames = { "name", "groupNumber", "examName", "examMark" };
    

    如果您想更改列中的标签,您可以创建一个映射到 link 带有所需标签的属性名称,并将其传递给列 header:

    Map<String, String> propertyToLabelMap = new HashMap<String, String>();
    propertyToLabelMap.put("name", "Full Name");
    propertyToLabelMap.put("groupNumber", "Group");
    propertyToLabelMap.put("examName", "Name");
    propertyToLabelMap.put("examMark", "Mark");
    
    ...
    
    DefaultColumnHeaderDataProvider defaultColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);