【发布时间】:2015-05-31 15:41:18
【问题描述】:
您好,我有一个关于 JavaFX 中的 TableView 的问题,并通过该对象的 getter 方法 使用模型中的对象 in 中的数据填充表,其中 是模型的一部分。
首先,这是我的模型:
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<Equipment> 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<Equipment> getEquipmentList() {
return equipmentList;
}
public void setEquipmentList(List<Equipment> 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 视图控制器:
public class CarmodelController implements Initializable {
CarmodelRepository carmodelRepository;
@FXML public TableView CarmodelTable;
@FXML public TableColumn<Carmodel,Integer> tableColumnID ;
@FXML public TableColumn<Carmodel,String> tableColumnLabel ;
@FXML public TableColumn<Carmodel, String> tableColumnManufacturer ;
@FXML public TableColumn<Carmodel,String> tableColumnCartype ;
public void initialize(URL location, ResourceBundle resources) {
carmodelRepository= new CarmodelRepository();
List<Carmodel> carmodelList= carmodelRepository.readAll();
ObservableList<Carmodel> carmodelObservableList = FXCollections.observableArrayList(carmodelList);
tableColumnID.setCellValueFactory(new PropertyValueFactory<Carmodel, Integer>("carmodelID"));
tableColumnLabel.setCellValueFactory(new PropertyValueFactory<Carmodel, String>("label"));
tableColumnManufacturer.setCellValueFactory(new PropertyValueFactory<Carmodel, String>("manufacturer")
这就是问题所在:
- 我可以在这里做一些像 PropertyValueFactory("manufacturer.getName()");这样就行不通了。它只是用内存地址填充表的列
所以我的问题是:
如何获取制造商的名称,通常在其他代码中,您可以通过调用方法来做到这一点:“manufacturer.getName();”它会为您提供带有制造商名称的字符串,但是当我将使用这些特定的汽车型号填充表格时,我该怎么做呢?
以及控制器代码的结尾(用值填充表格)。
CarmodelTable.setItems(carmodelObservableList);
}
提前谢谢你!
【问题讨论】:
标签: java javafx controller