【问题标题】:JavaFX TableView integer not displayingJavaFX TableView整数不显示
【发布时间】:2015-07-27 13:57:19
【问题描述】:

我在使用 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;
}

}

【问题讨论】:

    标签: java javafx tableview


    【解决方案1】:

    这可能是因为你有getInvetory() 方法而不是getInventory()

    来自PropertyValueFactory的文档:

    如果不存在与此模式匹配的方法,则支持尝试调用 get&lt;property&gt;()is&lt;property&gt;()(即上例中的 getFirstName() 或 isFirstName())。

    而且,您没有 getInventory()inventoryProperty() 的匹配模式。

    【讨论】:

    • 非常感谢,现在可以使用了。它总是我无法解决的最愚蠢的错误。
    • 欢迎来到 Stack Overflow。我很高兴这对您有所帮助。如果此答案解决了您的问题,请将其标记为已接受。
    猜你喜欢
    • 2018-11-13
    • 1970-01-01
    • 2016-09-02
    • 2014-07-28
    • 2015-12-15
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多