添加了对象,但它们未显示在表格中。并且分配了表中这两个添加对象的位置,但所有单元格都是空的。
并且有人可以建议关于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;
}
}
答案 0 :(得分:0)
您可以在没有默认配置的情况下创建NatTable实例。因此没有配置画家
在这里查看我们的入门教程:http://www.vogella.com/tutorials/NatTable/article.html
答案 1 :(得分:0)
替代
NatTable table = new NatTable(shell, gridLayer, false);
table.configure();
与
NatTable table = new NatTable(shell, gridLayer, true);
所以表自动配置,否则你必须自己指定配置。
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;
}
}
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);