TableView不显示内容

时间:2019-05-25 13:39:58

标签: java javafx

我试图通过在JavaFX中使用可观察的ArrayList插入数据,但是它不显示我在列表中插入的数据。而且编译器没有显示错误。

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

我希望输出中应该有数据,但是实际输出仅显示表和表列的内容,而不显示我放入可观察列表中的数据。

1 个答案:

答案 0 :(得分:0)

这部分

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"));

应该看起来像这样

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"));