![](/img/trans.png)
[英]JavaFX TableView Integer not displaying while String items are
[英]JavaFX TableView integer not displaying
我在JavaFX中遇到了tableview問題。
由於某些原因,我的TableView沒有顯示Inventory列,這是一個整數,我運行了一些測試及其原因,因為它沒有被執行的getInventory函數。
Java代碼
//Main Class
public class Main extends Application{
Stage window;
Scene scene1,scene2;
VBox panel;
TableView<Resources> table;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
//Stage
window = stage;
window.setTitle("Test Application - TableView");
//TableColumns
TableColumn<Resources, String> name_c = new TableColumn<Resources, String>("Resource Name");
name_c.setMinWidth(200);
name_c.setCellValueFactory( new PropertyValueFactory<>("Name"));
//---------------------------------------------
TableColumn<Resources, Double> price_c = new TableColumn<Resources, Double>("Resource Price");
price_c.setMinWidth(125);
price_c.setCellValueFactory( new PropertyValueFactory<>("Price"));
//---------------------------------------------
TableColumn<Resources, Integer> inventory_c = new TableColumn<Resources, Integer>("Resource Inventory");
inventory_c.setMinWidth(125);
inventory_c.setCellValueFactory( new PropertyValueFactory<Resources, Integer>("Inventory"));
//TableView
table = new TableView<>();
table.getColumns().addAll(name_c, price_c, inventory_c);
table.setItems(getResources());
//VBox
panel = new VBox();
panel.getChildren().add(table);
// Scene
Scene scene = new Scene(panel,500,250);
window.setScene(scene);
window.show();
}
public ObservableList<Resources> getResources(){
//Inserts into the TableView the Values
ObservableList<Resources> res = FXCollections.observableArrayList();
res.add( new Resources("Wood", 9.99, 100));
res.add( new Resources("Iron", 12.85, 70));
res.add( new Resources("Cotton", 3.16, 200));
res.add( new Resources("Marble", 20.75, 50));
res.add( new Resources("Glass", 5.99, 175));
return res;
}
}
//Resources Class
public class Resources {
private String name;
private double price;
private int inventory;
//Constructors
public Resources(){
this.name = "";
this.price = 0;
this.inventory = 0;
}
public Resources(String n,double p,int i){
this.name = n;
this.price = p;
this.inventory = i;
}
//Getters & Setters.
public String getName() {
return name;
}
public void setName(String nm) {
this.name = nm;
}
public double getPrice() {
return price;
}
public void setPrice(double pr) {
this.price = pr;
}
public int getInvetory() {
System.out.println(inventory);//Inventory Test (Data not displaying, works with the other get functions)
return inventory;
}
public void setInvetory(int inv) {
this.inventory = inv;
}
}
可能是因為你有getInvetory()
方法而不是getInventory()
。
從PropertyValueFactory的文檔:
如果不存在與此模式匹配的方法,則會嘗試調用
get<property>()
或is<property>()
(即上例中的getFirstName()或isFirstName())。
並且,您沒有匹配模式,即getInventory()
或inventoryProperty()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.