2017-06-25 169 views
0

任何人都可以幫助我如何填充表中的單元格嗎?我不知道我在哪裏做錯了。從對象填充表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; 
} 
+0

瞭解綁定 – Blip

回答

2

我實在看不出PartsList.java 的必要性,這不僅要填充您的實現代碼如下:

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

嗨哈利勒,謝謝 - 我管理不知怎麼做了成功的變化。然而,我沒有看到之前和現在寫的東西有太大差異 - 我有機會解釋爲什麼在我的情況下它沒有工作? BR –

+0

@HauptmanKoening p.getList()。get(0).getID()和p.getList()。get(0).getMake()和p.getList()。get(0).getMaterial這個方法將分別返回第一個itme int列表的字段,而它應該是屬性的字段本身的名稱 –