0

I have been trying out to create a tableview in javafx. For some reason, the tableview has the right columns, but doesn't have any data filled in it. Here's what i've tried so far. I have also tried the code on Eclipse and Intellij so I don't think there is any issue with my compiler or environment. Thanks for the help in advance. :)

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.*;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.*;


    public  class MainScreen extends Application{
            private TableView<Item> table = new TableView<Item>();
            private final ObservableList<Item> data =
                    FXCollections.observableArrayList(new Item("AZZZZ", "3"),new Item("ASD","33"));
            public static void main(String args[]) {
                launch(args);
            }

        public void start(Stage stage) {
            stage.setTitle("Inventory Management");
            FlowPane root = new FlowPane();


            table.setEditable(true);

            TableColumn<Item,String> nameCol = new TableColumn<>("Name");

            TableColumn<Item,String> qtyCol = new TableColumn<>("Quantity");
            nameCol.setMinWidth(100);
            nameCol.setCellValueFactory(
                    new PropertyValueFactory<>("name"));
            qtyCol.setMinWidth(100);
            qtyCol.setCellValueFactory(
                    new PropertyValueFactory<>("number"));


            //data.add(new Item("ZZZ","5"));
            table.setItems(data);
            table.getColumns().addAll(nameCol,qtyCol);

            root.getChildren().addAll(table);
            stage.setScene(new Scene(root,300,300));
            stage.show();
        }

        public static class Item{
            String name;
            String number;

            Item(String n,String num){
                name=n;
                number=num;
            }
        }
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
Shobhit Kumar
  • 626
  • 1
  • 5
  • 21
  • You need `public` getters for the fields in your `Item` class since `PropertyValueFactory` relies on them for retrieving the item for the column. – fabian Apr 09 '18 at 15:32
  • 1
    @fabian there should be an auto-close function for my-data-not-showing-in-the-table ;) At OP: please search this site, there are myriads of the exact same problem with about a handful of possible causes. – kleopatra Apr 09 '18 at 15:44

1 Answers1

1

You need to expose your data through getter/setter and properties, see https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.html

public static class Item {

    private final StringProperty nameProp = new SimpleStringProperty();
    private final StringProperty numberProp = new SimpleStringProperty();

    Item(String n, String num) {
        nameProp.set(n);
        numberProp.set(num);
    }

    public String getName() {
        return nameProp.get();
    }

    public void setName(String name) {
        nameProp.set(name);
    }

    public StringProperty nameProperty() {
        return nameProp;
    }

    public String getNumber() {
        return numberProp.get();
    }

    public void setNumber(String number) {
        numberProp.set(number);
    }

    public StringProperty numberProperty() {
        return numberProp;
    }
}
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • 1
    In this example properties aren't required, as `PropertyValueFactory` can use a getter if there is no required property found. – oshatrk Apr 10 '18 at 16:53