【问题标题】:Javafx: incompatible types: int cant be converted to IntegerPropertyJavafx:不兼容的类型:int 无法转换为 IntegerProperty
【发布时间】:2018-07-03 22:41:39
【问题描述】:

链接到完整项目 .zip(大学库存系统)https://www.dropbox.com/s/t88vsjt7awmlv93/MattNapper.zip?dl=0

尝试使用 ObservableList 获取要在 tableview 中显示的数据。零件模型是抽象的。 InHousePart 和 OutsoucredPart 扩展 Part。 Inventory 是我声明 ObservableList,伪造数据,返回 ObservableList 方法的地方

在我的 Inventory 课程中,我有:

public class Inventory {
    //declaring arrays for parts, and products
    private ObservableList<Product> products = FXCollections.observableArrayList();
    private ObservableList<Part> allParts = FXCollections.observableArrayList();



    //fake data
    //**This is where the error incompatible types: int cant be converted to Integer Property**
    InHousePart part1 = new InHousePart (1, 1, "pick", 10.00, 1, 1, 5 );

    //adds fake data to "allParts" arraylist
    public void addData() {
        allParts.add(part1);
    }

    public ObservableList<Part> getPartData() {
        return allParts;
    }

InHousePart 类

public class InHousePart extends Part{

    //constructors

    private IntegerProperty machineID;

    public IntegerProperty getMachineID() {
        return machineID;
    }

    public void setMachineID(IntegerProperty machineID) {
        this.machineID = machineID;
    }

    //constructor
    public InHousePart(IntegerProperty machineID, IntegerProperty partID, StringProperty name, DoubleProperty price, IntegerProperty inStock, IntegerProperty min, IntegerProperty max) {
        super(partID, name, price, inStock, min, max);
        this.machineID = machineID;
    }

零件类

public abstract class Part  {

    private IntegerProperty partID;
    private StringProperty name;
    private DoubleProperty price;
    private IntegerProperty inStock;
    private IntegerProperty min;
    private IntegerProperty max;

    public IntegerProperty getPartID() {
        return partID;
    }

    public void setPartID(IntegerProperty partID) {
        this.partID = partID;
    }

    public StringProperty getName() {
        return name;
    }

    public void setName(StringProperty name) {
        this.name = name;
    }

    public DoubleProperty getPrice() {
        return price;
    }

    public void setPrice(DoubleProperty price) {
        this.price = price;
    }

    public IntegerProperty getInStock() {
        return inStock;
    }

    public void setInStock(IntegerProperty inStock) {
        this.inStock = inStock;
    }

    public IntegerProperty getMin() {
        return min;
    }

    public void setMin(IntegerProperty min) {
        this.min = min;
    }

    public IntegerProperty getMax() {
        return max;
    }

    public void setMax(IntegerProperty max) {
        this.max = max;
    }

    //constructor
    public Part(IntegerProperty partID, StringProperty name, DoubleProperty price, IntegerProperty inStock, IntegerProperty min, IntegerProperty max) {
        this.partID = partID;
        this.name = name;
        this.price = price;
        this.inStock = inStock;
        this.min = min;
        this.max = max;
    }   
}

【问题讨论】:

  • 你真的希望1 会自动装箱到IntegerProperty吗?
  • java fx 游戏新手#beginner

标签: java javafx


【解决方案1】:

如果您想使用 JavaFX 属性,您应该遵循tutorial 中描述的模式。基本上,由IntegerProperty 实现的属性就其定义的get/set 方法而言应该看起来就像JavaBean int 属性。所以你的Part 类应该包含以下API:

public int getPartID() ;
public void setPartID(int partID);

由于您使用可观察属性来实现这两个方法,您应该以及使用该方法提供对该属性对象的访问权限

public IntegerProperty partIDProperty();

一般模式是,对于名称为prop、类型为T 的属性,您应该有方法

public T getProp() ;
public void setProp(T prop) ;
public ObjectProperty<T> propProperty();

(当T为原始类型时,最后一个方法的返回类型会略有不同。

另请参阅When to use JavaFX properties setter and getter, instead of using the property directly 进行类似讨论。

所以你应该有

public class Part {

    private final IntegerProperty partID = new SimpleIntegerProperty() ;

    public IntegerProperty partIDProperty() {
        return partID ;
    }

    public final int getPartID() {
        return partIDProperty().get();
    }

    public final void setPartID(int partID) {
        partIDProperty().set(partID);
    }

    // similarly for all other properties

}

这里有一个基本约定,getPartID() 应该返回与partIDProperty().get() 相同的值:使getset 方法final 确保即使类是子类也是如此。

最后,构造函数应该为values而不是properties获取参数。所以你的构造函数应该是

public Part(int partID, String name, double price, int inStock, int min, int max) {
    setPartID(partID);
    setName(name);
    setPrice(price);
    setInStock(inStock);
    setMin(min);
    setMax(max);
}   

请注意,从构造函数调用这些 set 方法是安全的,因为它们是 final。 (如果您愿意,也可以只使用partIDProperty.set(partID) 等。)

【讨论】:

  • 这帮了很多忙,但我的主页部件表中仍然没有任何数据。
猜你喜欢
  • 2017-10-03
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 2018-05-28
  • 2017-03-27
  • 2017-02-13
  • 1970-01-01
相关资源
最近更新 更多