Java FX使用特定模型填充tableview

您好我在JavaFX中有一个关于TableView的问题,并通过此对象的getter方法使用模型中对象的数据填充表,该对象模型的一部分

首先,这是我的模型:

package model; import java.util.List; public class Carmodel { private int carmodelID; private Cartype cartype; private Manufacturer manufacturer; private DrivingLicense drivingLicense; private String label; private int seats; private int kw; private String fuelType; private double priceDay; private double priceKM; private int axes; private int loadVolume; private int loadCapacity; private List equipmentList; public Carmodel() { } public int getCarmodelID() { return carmodelID; } public void setCarmodelID(int carmodelID) { this.carmodelID = carmodelID; } public Cartype getCartype() { return cartype; } public void setCartype(Cartype cartype) { this.cartype = cartype; } public Manufacturer getManufacturer() { return manufacturer; } public void setManufacturer(Manufacturer manufacturer) { this.manufacturer = manufacturer; } public DrivingLicense getDrivingLicense() { return drivingLicense; } public void setDrivingLicense(DrivingLicense drivingLicense) { this.drivingLicense = drivingLicense; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public int getSeats() { return seats; } public void setSeats(int seats) { this.seats = seats; } public int getKw() { return kw; } public void setKw(int kw) { this.kw = kw; } public String getFuelType() { return fuelType; } public void setFuelType(String fuelType) { this.fuelType = fuelType; } public double getPriceDay() { return priceDay; } public void setPriceDay(double priceDay) { this.priceDay = priceDay; } public double getPriceKM() { return priceKM; } public void setPriceKM(double priceKM) { this.priceKM = priceKM; } public int getAxes() { return axes; } public void setAxes(int axes) { this.axes = axes; } public int getLoadVolume() { return loadVolume; } public void setLoadVolume(int loadVolume) { this.loadVolume = loadVolume; } public int getLoadCapacity() { return loadCapacity; } public void setLoadCapacity(int loadCapacity) { this.loadCapacity = loadCapacity; } public List getEquipmentList() { return equipmentList; } public void setEquipmentList(List equipmentList) { this.equipmentList = equipmentList; } 

如您所见,有一个特定的成员(私人制造商制造商)它是“制造商”类型的对象。 而制造商类看起来像这样:

 public class Manufacturer { private int manufacturerID; private String name; public Manufacturer() { } public int getManufacturerID() { return manufacturerID; } public void setManufacturerID(int manufacturerID) { this.manufacturerID = manufacturerID; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

这是我的JavaFX View控制器:

 public class CarmodelController implements Initializable { CarmodelRepository carmodelRepository; @FXML public TableView CarmodelTable; @FXML public TableColumn tableColumnID ; @FXML public TableColumn tableColumnLabel ; @FXML public TableColumn tableColumnManufacturer ; @FXML public TableColumn tableColumnCartype ; public void initialize(URL location, ResourceBundle resources) { carmodelRepository= new CarmodelRepository(); List carmodelList= carmodelRepository.readAll(); ObservableList carmodelObservableList = FXCollections.observableArrayList(carmodelList); tableColumnID.setCellValueFactory(new PropertyValueFactory("carmodelID")); tableColumnLabel.setCellValueFactory(new PropertyValueFactory("label")); tableColumnManufacturer.setCellValueFactory(new PropertyValueFactory("manufacturer") 

这是问题所在:

  1. 我可以在这里做一些像PropertyValueFactory(“manufacturer.getName()”); 这样它就行不通了。 它只是用内存地址填充表的列

所以我的问题是:

如何获取制造商的名称,通常,在其他代码中,您可以通过调用方法来执行此操作:“manufacturer.getName();” 它会给你带有制造商名称的字符串,但是如何用这些特定的carmodel填充表格呢?

并且控制器代码结束(用表填充表格)。

  CarmodelTable.setItems(carmodelObservableList); } 

先感谢您!

你可以做

 tableColumnManufacturer.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getManufacturer().getName()); 

setCellValueFactory方法需要Callback, ObservableValue>对象。 因此,此代码中的CellDataFeaturesCellDataFeatures对象,而cellData.getValue()为该行提供CarModel对象。 然后cellData.getValue().getManufacturer().getName()给出你想要的值; 您只需将其包装在ReadOnlyObservableWrapper以获取包含该值的ObservableValue