任何人都可以幫助我如何填充表中的單元格嗎?我不知道我在哪裏做錯了。從對象填充表Javafx
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);
}
部件類代碼:
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;
}
和PARTLIST類
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;
}
瞭解綁定 – Blip