将嵌套对象属性绑定到JavaFx中的TableView

Jor*_*rez 9 java javafx

我有下一堂课

public class ProductStockDto extends 

    private Long id;
    private Long amount;
    private ProductDto product;
    private StockDto stock;

    //getters and setters...
}
Run Code Online (Sandbox Code Playgroud)

在JavaFx中我有我的表,我想将product.name属性绑定到列,就像这样.

ObservableList<ProductStockDto> data = FXCollections.observableArrayList();
data.addAll(products);
nameColumn.setCellValueFactory(new PropertyValueFactory("product.name"));
productTable.setItems(data);
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,TableView上的行显示为空白.

有人可以帮我这个吗?我想绑定嵌套对象属性,在Java Swing上就像是$ {product.name}

谢谢.

Dac*_*ch0 9

我遇到了同样的问题,无法以任何建议的方式使其工作,但是如果我们实例化列并具有嵌套属性,例如,在类 Employee 中,对我来说有什么技巧:

@FXML
private TableColumn<Employee, String> columnEmpName;
@FXML
private TableColumn<Employee, String> columnEmpLastName;
@FXML
private TableColumn<Employee, String> columnEmpJobDesc;
Run Code Online (Sandbox Code Playgroud)

类是:

public class Employee {
   private String name;
   private String lastName;
   .
   .
   private EmployeeJobDescription jobDesc; //this one is nested
}
Run Code Online (Sandbox Code Playgroud)

我已经用ObservableValue<String>我为那个财产捡到的绳子做了

columnEmpName.setCellValueFactory(new PropertyValueFactory<Employee, String>("name"));
columnEmpLastName.setCellValueFactory(new PropertyValueFactory<Employee, String>("lastname"));
columnEmpJobDesc.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getJobDesc().getJobDescription()));
Run Code Online (Sandbox Code Playgroud)

EmployeeJobDescription 将与所有 getter 和 setter 一起使用

public class EmployeeJobDescription {

private Integer id;
private String jobDescription;

public EmployeeJobDescription(Integer id) {
    this.id = id;
}

public EmployeeJobDescription(Integer id, String jobDescription) {
    this.id = id;
    this.jobDescription = jobDescription;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getJobDescription() {
    return jobDescription;
}

public void setJobDescription(String jobDescription) {
    this.jobDescription = jobDescription;
}

}
Run Code Online (Sandbox Code Playgroud)

在这篇文章中找到了如何做到这一点:

将字符串转换为 ObservableValue


Ita*_*iha 5

不支持此格式Javafx,作为解决方法,您可以尝试这样的事情:

nameColumn.setCellValueFactory(new Callback<CellDataFeatures<ProductStockDto, String>, 
                                                         ObservableValue<String>>() {  
    @Override  
    public ObservableValue<String> call(CellDataFeatures<ProductStockDto, String> data){  
         return data.getValue().getProducts().nameProperty();  
    }  
});  
Run Code Online (Sandbox Code Playgroud)

哪里ProductDto会有

public class ProductDto{

    private StringProperty name = new SimpleStringProperty("Itachi");

    public String getName() {
        return name.get();
    }

    public void setStreet(String name) {
        this.name.set(name);
    }

    public StringProperty nameProperty(){
        return name;
    }
}
Run Code Online (Sandbox Code Playgroud)


Dmy*_*nko 5

作为变体,您可以使用合成属性。

让我们将其命名为“productName”:

nameColumn.setCellValueFactory(new PropertyValueFactory("productName"));
Run Code Online (Sandbox Code Playgroud)

在您的 ProductStockDto 类中有这样的东西:

public String getProductName() {
   return product.getName();
}
Run Code Online (Sandbox Code Playgroud)