1

Could anybody help me how to populate the cells in Table? I don't know where I am doing it wrong.

public class SpareParts extends Application {
private ObservableList<Part> oblist;

@Override
public void start(Stage primaryStage) throws Exception {
    Group root = new Group();
    Scene scene = new Scene(root, 600, 400);
    root.getChildren().add(table());
    primaryStage.setTitle("doing things");
    primaryStage.setScene(scene);
    primaryStage.show();

}

private TableView table() {
    final TableView table = new TableView<>();
    final Label label = new Label("Table list");
    label.setFont(new Font("Arial", 10));
    PartsList p = new PartsList();
    p.addToList();

    table.setEditable(true);

    TableColumn identification = new TableColumn("ID");
    identification.setCellFactory(new PropertyValueFactory<>(p.getList().get(0).getID()));
    TableColumn make = new TableColumn("Make");
    make.setCellFactory(new PropertyValueFactory<>(p.getList().get(0).getMake()));
    TableColumn material = new TableColumn("Material");
    material.setCellFactory(new PropertyValueFactory<>(p.getList().get(0).getMaterial()));
    table.setItems(p.getList());
    table.getColumns().addAll(identification, make, material);
    return table;
}

public static void main(String[] args) {
    launch(args);

}

Part class code:

public class Part {
private String ID;
private String make;
private String material;

public Part(String iD, String make, String material) {
    super();
    ID = iD;
    this.make = make;
    this.material = material;
}

public String getID() {
    return ID;
}

public String getMake() {
    return make;
}

public String getMaterial() {
    return material;
}

and partList class

public class PartsList {
private ObservableList<Part> list=FXCollections.observableArrayList();
public  void addToList() {
    Part a=new Part("1", "make", "material");
    Part b=new Part("2", "make", "material");
    Part c=new Part("3", "make", "material");
    Part d=new Part("4", "make", "material");
    list.add(a);
    list.add(b);
    list.add(c);
    list.add(d);

}
public ObservableList<Part> getList() {
    return list;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

3

I can't really see the necessity of PartsList.java This only should populate your tableview:

private TableView<Part> tableView = new TableView<Part>();
private TableColumn<Part, String> make= new TableColumn<Part, String>();
private TableColumn<Part, String> material= new TableColumn<Part, String>(); 
private ObservableList<Part> list=FXCollections.observableArrayList();
//here initialise list or anything
make.setCellValueFactory(new PropertyValueFactory<Part, String>("make"));
material.setCellValueFactory(new PropertyValueFactory<Part, String>("material"));
tableView.setItems(list);
Khalil M
  • 1,788
  • 2
  • 22
  • 36
  • Hi Khalil, thanks - i managed somehow to make the changes successfully. However, i dont see too big difference in what was writen before and now - any chance i get explanation why in my case it didnt work? BR – Hauptman Koening Jun 26 '17 at 07:03
  • @HauptmanKoening p.getList().get(0).getID() and p.getList().get(0).getMake() and p.getList().get(0).getMaterial() are wrong approach this will return respectively the first 's itme int the list's fields while it should be a property the name of the field itself – Khalil M Jun 26 '17 at 08:56