有人可以帮我如何填充表格中的单元格吗?我不知道我做错了什么。
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;
}
答案 0 :(得分:2)
我无法真正看到PartsList.java的必要性 这只应该填充你的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);