0

I am trying to insert data by using observable ArrayList in JavaFX, but it does not show the data that I insert in the list. And the compilers does not show the error.

public class EquipmentClass {

    // declare instance variable of Equipment
    private String brandEquipment;
    private String modelEquipment;
    private String typeEquipment;
    private String colorEquipment;
    private int lengthEquipment;
    private int widthEquipment;
    private int heightEquipment;
    private int capacityEquipment;
    private String user;
    private String imageURL;

 // constructor brandEquipment, modelEquipment, typeEquipment, colorEquipment, lengthEquipment, widthEquipment, HeightEquipment, capacityEquipment, user
    public EquipmentClass(String brandEquipment, String modelEquipment,String typeEquipment, String colorEquipment, int lengthEquipment, int widthEquipment, int heightEquipment, int capacityEquipment,String imageURL) {
        this.brandEquipment = brandEquipment;
        this.modelEquipment = modelEquipment;
        this.typeEquipment = typeEquipment;
        this.colorEquipment = colorEquipment;
        this.lengthEquipment = lengthEquipment;
        this.widthEquipment = widthEquipment;
        this.heightEquipment = heightEquipment;
        this.capacityEquipment = capacityEquipment;

        this.imageURL = imageURL;
    }




    // getter & setter methods
    public String getBrandEquipment() {
        return brandEquipment;
    }

    public void setBrandEquipment(String brandEquipment) {
        this.brandEquipment = brandEquipment;
    }

    public String getModelEquipment() {
        return modelEquipment;
    }

    public void setModelEquipment(String modelEquipment) {
        this.modelEquipment = modelEquipment;
    }

    public String getTypeEquipment() {
        return typeEquipment;
    }

    public void setTypeEquipment(String typeEquipment) {
        this.typeEquipment = typeEquipment;
    }

    public String getColorEquipment(){
        return colorEquipment;
    }

    public void setColorEquipment (String colorEquipment) {
        this.colorEquipment = colorEquipment;
    }

    public int getLengthEquipment() {
        return lengthEquipment;
    }

    public void setLengthEquipment (int lengthEquipment) {
        this.lengthEquipment = lengthEquipment;
    }

    public int getWidthEquipment() {
        return widthEquipment;
    }

    public void setWidthEquipment (int widthEquipment) {
        this.widthEquipment = widthEquipment;
    }

    public int getHeightEquipment() {
        return heightEquipment;
    }

    public void setHeightEquipment (int heightEquipment){
        this.heightEquipment = heightEquipment;
    }

    public int getCapacityEquipment() {
        return capacityEquipment;
    }

    public void setCapacityEquipment (int capacityEquipment) {
        this.capacityEquipment = capacityEquipment;
    }

    public void setImageURL (String imageURL){
        this.imageURL = imageURL;
    }

    public String getImageURL(){
        return imageURL;
    }


    public String toString() {
        return this.getClass().getSimpleName();
    }
}

private ObservableList<EquipmentClass> getUserList() {

    EquipmentClass user1 = new EquipmentClass("gh", "tim", "Sg", "Smith", 1,3,9,2," t");

    EquipmentClass user2 = new EquipmentClass("smith", "sm","k", "S8h", 15,13,7,2," t");

    EquipmentClass user3 = new EquipmentClass("smith", "sm", "Sh", "Shh", 8,5,78,8," t");


    ObservableList<EquipmentClass> list = FXCollections.observableArrayList(user1, user2, user3);
    return list;
}
TableView<EquipmentClass> table = new TableView<EquipmentClass>();

TableColumn<EquipmentClass,String>brand= new TableColumn<EquipmentClass,String>("Brand");

// Create column UserName (Data type of String).
TableColumn<EquipmentClass, String> model //
        = new TableColumn<EquipmentClass, String>("Model");

TableColumn<EquipmentClass, String> type //
        = new TableColumn<EquipmentClass, String>("Type");

TableColumn<EquipmentClass, String> color //
        = new TableColumn<EquipmentClass, String>("Colour");

TableColumn<EquipmentClass, Integer> length //
        = new TableColumn<EquipmentClass, Integer>("Length");

TableColumn<EquipmentClass, Integer> width //
        = new TableColumn<EquipmentClass, Integer>("Width");

TableColumn<EquipmentClass, Integer> height //
        = new TableColumn<EquipmentClass, Integer>("Height");

TableColumn<EquipmentClass, Integer> capacity //
        = new TableColumn<EquipmentClass, Integer>("Capacity");

TableColumn<EquipmentClass, String> imageURL //
        = new TableColumn<EquipmentClass, String>("Image");


brand.setCellValueFactory(new PropertyValueFactory<>("brand"));
model.setCellValueFactory(new PropertyValueFactory<>("model"));
type.setCellValueFactory(new PropertyValueFactory<>("type"));
color.setCellValueFactory(new PropertyValueFactory<>("color"));
length.setCellValueFactory(new PropertyValueFactory<>("length"));
width.setCellValueFactory(new PropertyValueFactory<>("width"));
height.setCellValueFactory(new PropertyValueFactory<>("height"));
capacity.setCellValueFactory(new PropertyValueFactory<>("cap"));
imageURL.setCellValueFactory(new PropertyValueFactory<>("img"));


// Display row data
ObservableList<EquipmentClass> list = getUserList();
table.setItems(list);
table.getColumns().addAll(brand,model,type,color,length,width,height,capacity,imageURL);

I expect the output should have data, but the actual output only shows the table and the content of table column, does not show the data that I put in the observable list.

fabian
  • 80,457
  • 12
  • 86
  • 114
Yun Song
  • 39
  • 4

1 Answers1

0

This part

brand.setCellValueFactory(new PropertyValueFactory<>("brand"));
    model.setCellValueFactory(new PropertyValueFactory<>("model"));
    type.setCellValueFactory(new PropertyValueFactory<>("type"));
    color.setCellValueFactory(new PropertyValueFactory<>("color"));
    length.setCellValueFactory(new PropertyValueFactory<>("length"));
    width.setCellValueFactory(new PropertyValueFactory<>("width"));
    height.setCellValueFactory(new PropertyValueFactory<>("height"));
    capacity.setCellValueFactory(new PropertyValueFactory<>("cap"));
    imageURL.setCellValueFactory(new PropertyValueFactory<>("img"));

should look like this

brand.setCellValueFactory(new PropertyValueFactory<>("brandEquipment"));
    model.setCellValueFactory(new PropertyValueFactory<>("modelEquipment"));
    type.setCellValueFactory(new PropertyValueFactory<>("typeEquipment"));
    color.setCellValueFactory(new PropertyValueFactory<>("colorEquipment"));
    length.setCellValueFactory(new PropertyValueFactory<>("lengthEquipment"));
    width.setCellValueFactory(new PropertyValueFactory<>("widthEquipment"));
    height.setCellValueFactory(new PropertyValueFactory<>("heightEquipment"));
    capacity.setCellValueFactory(new PropertyValueFactory<>("capacityEquipment"));
    imageURL.setCellValueFactory(new PropertyValueFactory<>("imageURL"));
micpog90
  • 106
  • 8
  • *"In the PropertyValueFactory you have to use the same variable names, which you used in the EquipmentClass."* In this case this may work, but the statement is incorrect. `PropertyValueFactory` accesses the data via methods only (either through the property getter [`nameProperty()`] or if none matching the property name exists using the getter [`getName()`]; `name` is the content of the string passed to the constructor). – fabian May 25 '19 at 15:07
  • Thanks for pointing out..I deleted this part – micpog90 May 25 '19 at 15:22
  • color.setCellValueFactory(cellData->cellData.getValue().getColorEquipment()); I tried to use this code to get the value but it shows is a bad return typein lambda expression:String cannot be converted to Observable value .Why? – Yun Song May 26 '19 at 02:47