Java FX使用特定模型填充tableview
您好,我有一个关于JavaFX中的TableView的问题,我想通过该对象的getter方法,用该对象中的对象的数据填充该表,该对象是该模型的一部分 首先,这是我的模型:Java FX使用特定模型填充tableview,java,javafx,controller,Java,Javafx,Controller,您好,我有一个关于JavaFX中的TableView的问题,我想通过该对象的getter方法,用该对象中的对象的数据填充该表,该对象是该模型的一部分 首先,这是我的模型: package model; import java.util.List; public class Carmodel { private int carmodelID; private Cartype cartype; private Manufacturer manufacturer;
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;
}
这是我的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")
提前谢谢你 你能行
tableColumnManufacturer.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue().getManufacturer().getName());
setCellValueFactory
方法需要一个回调
对象。因此,此代码中的cellData
是一个CellDataFeatures
对象,而cellData.getValue()
为行提供CarModel
对象。然后cellData.getValue().getManufacturer().getName()
给出所需的值;您只需将其包装在一个ReadOnlyObservableWrapper
中,即可获得包含该值的ObservableValue
。谢谢:)这对我很有帮助!
CarmodelTable.setItems(carmodelObservableList);
}
tableColumnManufacturer.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue().getManufacturer().getName());