【问题标题】:Java FX populate tableview with specific modelJava FX 使用特定模型填充 tableview
【发布时间】: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") 

这就是问题所在:

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

所以我的问题是:

如何获取制造商的名称,通常在其他代码中,您可以通过调用方法来做到这一点:“manufacturer.getName();”它会为您提供带有制造商名称的字符串,但是当我将使用这些特定的汽车型号填充表格时,我该怎么做呢?

以及控制器代码的结尾(用值填充表格)。

    CarmodelTable.setItems(carmodelObservableList);

}

提前谢谢你!

【问题讨论】:

    标签: java javafx controller


    【解决方案1】:

    你可以的

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

    setCellValueFactory 方法需要一个 Callback&lt;CellDataFeatures&lt;Carmodel, String&gt;, ObservableValue&lt;String&gt;&gt; 对象。因此,此代码中的cellDataCellDataFeatures&lt;Carmodel, String&gt; 对象,cellData.getValue() 为行提供CarModel 对象。然后cellData.getValue().getManufacturer().getName() 给出你想要的值;您只需将其包装在 ReadOnlyObservableWrapper 中即可获得包含该值的 ObservableValue&lt;String&gt;

    【讨论】:

    • 谢谢 :) 这对我有帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 2019-01-13
    • 2014-01-17
    • 1970-01-01
    相关资源
    最近更新 更多