Javafx TreeTableView将嵌套值添加到TreeItemPropertyValueFactory
我需要在我的TreeTableView中添加两列(“Id”和“Workplace”)的内容。 我不知道怎么做,因为我无法从Manager->ArrayList中获取嵌套值。 如果内容的类型只能是字符串,我应该在TreeItemPropertyValueFactory中传递什么 代码的其余部分工作正常。 如果有任何帮助,我将不胜感激Javafx TreeTableView将嵌套值添加到TreeItemPropertyValueFactory,javafx,treetableview,Javafx,Treetableview,我需要在我的TreeTableView中添加两列(“Id”和“Workplace”)的内容。 我不知道怎么做,因为我无法从Manager->ArrayList中获取嵌套值。 如果内容的类型只能是字符串,我应该在TreeItemPropertyValueFactory中传递什么 代码的其余部分工作正常。 如果有任何帮助,我将不胜感激 公共类管理器{ 私有字符串管理器名称; 私有列表管理器staff=new ArrayList(); 公共管理器(字符串管理器名称、列表管理器人员){ this.m
公共类管理器{
私有字符串管理器名称;
私有列表管理器staff=new ArrayList();
公共管理器(字符串管理器名称、列表管理器人员){
this.managersName=managersName;
this.managersStaff=managersStaff;
}
公共字符串getManagerName(){
返回managersName;
}
public void setManagersName(字符串managersName){
this.managersName=managersName;
}
公共列表GetManagerStaff(){
返回管理人员;
}
public void setManagersStaff(列出managersStaff){
this.managersStaff=managersStaff;
}
}
您可以这样做
columnStaffId.setCellValueFactory(cellData -> {
TreeItem<?> item = cellData.getValue();
Object data = item.getValue();
if (data instanceof Employee) {
Employee employee = (Employee)data ;
return new SimpleStringProperty(employee.getId());
} else {
return new SimpleStringProperty("");
}
});
您的设置有点奇怪,因为您声明了一个TreeTableView
,但其中一些项不包含Manager
s,而是Employee
s。因此,无法保证您不会在此处的某些位置抛出ClassCastException
s,或者由于TreeItemPropertyValueFactory
试图在非管理器的对象上调用getManagerName()
而导致的其他错误
您可能希望重构,以便使用TreeTableView
,或者重构模型,以便Manager
和Employee
都是其他类的子类(然后将其用作TreeTableView
的类型)。非常感谢!我希望它能正常工作。我只是想避免创建下一个类,从另外两个Clesses收集字段。但如果你看到任何问题,我会做的。现在就像一个符咒!请不要发布文本截图。好的,但如何在代码中标记比哪一行包含我的问题更准确的内容??行没有编号,整个代码在屏幕截图下方。请在代码中添加注释,类似于/>>>>>>>>这里是重要部分
public class Employee {
private String id;
private String workplace;
public Employee(String id, String workplace) {
this.id = id;
this.workplace = workplace;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
this.workplace = workplace;
}
}
public class Manager {
private String managersName;
private List<Employee> managersStaff = new ArrayList<>();
public Manager(String managersName, List<Employee> managersStaff) {
this.managersName = managersName;
this.managersStaff = managersStaff;
}
public String getManagersName() {
return managersName;
}
public void setManagersName(String managersName) {
this.managersName = managersName;
}
public List<Employee> getManagersStaff() {
return managersStaff;
}
public void setManagersStaff(List<Employee> managersStaff) {
this.managersStaff = managersStaff;
}
}
columnStaffId.setCellValueFactory(cellData -> {
TreeItem<?> item = cellData.getValue();
Object data = item.getValue();
if (data instanceof Employee) {
Employee employee = (Employee)data ;
return new SimpleStringProperty(employee.getId());
} else {
return new SimpleStringProperty("");
}
});
columnStaffId.setCellValueFactory(cellData -> {
if (cellData.getValue().getValue() instanceof Employee employee) {
return new SimpleStringProperty(employee.getId());
} else {
return new SimpleStringProperty("");
}
});