不显示NatTable中的数据

时间:2018-04-21 09:51:57

标签: java swt nattable

添加了对象,但它们未显示在表格中。并且分配了表中这两个添加对象的位置,但所有单元格都是空的。

并且有人可以建议关于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 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;
  }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

  1. 替代

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

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

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

  2. DefaultBodyDataProvider使用反射从指定的类中获取数据,因此Student必须是正确的bean类,因此它必须是public并且必须具有getter对于它的属性。如果您还想检索Exam属性,可以在“学生”中为他们创建获取者:

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

    如果要更改列中的标签,可以创建一个地图,将属性名称与所需标签链接,并将其传递给列标题:

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