1

i'm having problems with a tableview in JavaFX.

For some reason my TableView it's not displaying the Inventory column which is an integer, i've run some test & the reason it's because the getInventory function it's not being executed.

Java Code

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

}
0

1 Answer 1

3

It could be because you have getInvetory() method rather than getInventory().

From the documentation of PropertyValueFactory:

If no method matching this pattern exists, there is fall-through support for attempting to call get<property>() or is<property>() (that is, getFirstName() or isFirstName() in the example above).

And, you don't have the matching pattern which is either getInventory() or inventoryProperty().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot, it's working now. It's always the dumbest error the one i can't solve.
Welcome to Stack Overflow. I am glad that this helped you. If this answer solved your issue, please mark it as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.