2

Hello I have got a question about TableView in JavaFX and populating the table with data from an object in the model via a getter method of this object, which is part of the model .

First of all, here is my model:

   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;
}

As you can see there is a specific member (private Manufacturer manufacturer) It is an object from the type "Manufacturer". And the Manufacturer class looks like this:

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;
}
}

This is my controller for the 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") 

And here is the problem:

  1. Can I do here something like PropertyValueFactory("manufacturer.getName()"); This way it didn't work. It just populate the column of the table with memory adress

So my question is:

How can I get the name of the manufacturer, normally, in other code, you can do this by calling the method: "manufacturer.getName();" and it will give you the String with the name of the manufacturer, but how can I do this while I will populate the table with these specific carmodels?

And the end of the controller code ( filling the Table with values).

    CarmodelTable.setItems(carmodelObservableList);

}

Thank you in advance!

1 Answer 1

1

You can do

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

The setCellValueFactory method expects a Callback<CellDataFeatures<Carmodel, String>, ObservableValue<String>> object. Hence cellData in this code is a CellDataFeatures<Carmodel, String> object, and cellData.getValue() gives the CarModel object for the row. Then cellData.getValue().getManufacturer().getName() gives the value you want; you just have to wrap it in a ReadOnlyObservableWrapper to get an ObservableValue<String> containing that value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.