Javafx:不兼容的类型:int无法转换为IntegerProperty [英] Javafx: incompatible types: int cant be converted to IntegerProperty

查看:230
本文介绍了Javafx:不兼容的类型:int无法转换为IntegerProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

link to full project .zip (Inventory System for College) https://www.dropbox.com/s/t88vsjt7awmlv93/MattNapper.zip?dl=0

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

Trying to get data to show in tableview using ObservableList. Part Model is an abstract. InHousePart, and OutsoucredPart extend Part. Inventory is where I declare ObservableList, fake data, return ObservableList method

在我的库存类我有:

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 Class

InHousePart Class

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

Part Class

Part Class

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


推荐答案

如果如果要使用JavaFX属性,则应遵循教程。基本上,根据get / set方法,由 IntegerProperty 实现的属性应该看起来就像JavaBean int 属性它定义了。因此,您的 Part 类应包含以下API:

If you want to use JavaFX properties, you should follow the pattern described in the tutorial. Basically, a property implemented by an IntegerProperty should look just like a JavaBean int property in terms of the get/set methods it defines. So your Part class should include the following API:

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

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

Since you're using an observable property to implement these two methods, you should provide access to that property object as well, using the method

public IntegerProperty partIDProperty();

一般模式是,对于具有名称的属性,例如 prop ,类型 T ,你应该有方法

The general pattern is, that for a property with name, say, prop, of type T, you should have methods

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

(当 T 是基本类型时,最后一个方法的返回类型会略有不同。)

(when T is a primitive type, the return type of the last method will be slightly different).

另见何时使用JavaFX属性setter和getter,而不是直接使用属性进行类似的讨论。

Also see When to use JavaFX properties setter and getter, instead of using the property directly for a similar discussion.

所以你应该

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() :使获取设置方法 final 确保即使该类是子类也是如此。

There is a basic contract here that getPartID() should return the same value as partIDProperty().get(): making the get and set methods final ensures this is true even if the class is subclassed.

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

Finally, the constructor should take parameters for the values, not for the properties. So your constructor should be

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方法是安全的,因为它们是是最终。 (如果需要,你也可以只做 partIDProperty.set(partID)等。)

Note that it is safe to call those set methods from the constructor, because they are final. (You could also just do partIDProperty.set(partID), etc., if you wanted.)

这篇关于Javafx:不兼容的类型:int无法转换为IntegerProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆