Java FX使用特定模型填充tableview

时间:2015-05-31 15:41:18

标签: java javafx controller

您好我在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 View控制器:

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()”)的事情;这样它就行不通了。它只是用内存地址
  2. 填充表格的列

    所以我的问题是:

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

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

        CarmodelTable.setItems(carmodelObservableList);
    
    }
    

    提前谢谢!

1 个答案:

答案 0 :(得分:1)

你可以做到

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

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