java - 从对象填充表 Javafx

标签 java javafx populate

有人可以帮我如何填充表格中的单元格吗?我不知道我哪里做错了。

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

最佳答案

我真的看不出 PartsList.java 的必要性 这只会填充您的表格 View :

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);

关于java - 从对象填充表 Javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44743886/

相关文章:

JavaFX 获取特定 Pane 大小

java - 如何在 Javafx2 中选择多个文件夹和文件?

java - 填充时只设置从表单到操作的最后一个参数,为什么?

javascript - 从不谈论填充普通 View

java - 在 Java 中将视频分割成更小的定时片段

javafx - 如何在 JavaFx textArea 中附加多色文本

java - 如何将 POST 表单数据从 HTML 传递到 Java Rest Api

mysql - 通过 Perl(或其他语言)中的 ftp 递归远程目录树,仅将文件层次结构信息填充到 MySQL 中 - 查找现有库

java - tr Locale、Windows 7 和 Linux 中的日期格式给出 2 个结果

java - Eclipse 重构