java - TableView不显示内容

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

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

最佳答案

这部分

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

关于java - TableView不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305357/

相关文章:

Java 没有找到合适的 addListener 方法

image - JavaFX 按钮背景图像

Java:如何创建 Class<T> 参数?

Java Socket,小字符串不必要地分成两个数据包

java - 获取括号之间的字符串

java - 在Spring MVC中将复选框值插入数据库

java - 在JavaFx中,如何从网格 Pane 中删除特定节点及其坐标?

java - HiveContext createDataFrame 不适用于 pySpark (jupyter)

DirectoryChooser - 多个目录

java - 如何在显示窗口之前更改 JavaFX 中的 FXML Controller 变量值?