Javafx:不兼容的类型:无法将int转换为IntegerProperty
链接到完整的project.zip(大学库存系统) 尝试使用ObservableList获取要在tableview中显示的数据。零件模型是一种抽象模型。内部零件和外部零件延伸零件。库存是我声明ObservableList、伪造数据、返回ObservableList方法的地方 在我的库存类中,我有:Javafx:不兼容的类型:无法将int转换为IntegerProperty,java,javafx,Java,Javafx,链接到完整的project.zip(大学库存系统) 尝试使用ObservableList获取要在tableview中显示的数据。零件模型是一种抽象模型。内部零件和外部零件延伸零件。库存是我声明ObservableList、伪造数据、返回ObservableList方法的地方 在我的库存类中,我有: public class Inventory { //declaring arrays for parts, and products private ObservableList&l
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;
}
半类
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;
}
}
如果要使用JavaFX属性,应该遵循中描述的模式。基本上,由
IntegerProperty
实现的属性就其定义的get/set方法而言应该与JavaBeanint
属性类似。因此,您的部分
类应该包括以下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();
这里有一个基本约定,即getPartID()
应返回与partIDProperty()相同的值。get()
:使get
和set
方法final
确保即使该类是子类,也是如此
最后,构造函数应该为值而不是属性获取参数。所以你的构造函数应该是
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
。(如果你愿意,你也可以只做partiproperty.set(partID)
,等等。)你真的期望1
会自动装箱到IntegerProperty
?java fx游戏新手,这帮了大忙,但我的主页上仍然没有任何数据。
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);
}