我想在JavaFX中使用带有List的TableView。它工作到90%。
我的问题是,如果我将数据加载到TableView的第二行,我的第一行是相同的...我认为我的foreach中存在问题。
我的代码:
@FXML
private TableView<Ticket> tblTicket = new TableView<>();
...
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
...
tblTicket.getColumns().addAll(Ticket.getColumn(tblTicket));
tblTicket.setItems(getTicketData());
} catch (Exception e) {
...
}
}
...
public ObservableList<Ticket> getTicketData() {
//Eine Liste mit allen Tickets
ObservableList<Ticket> ticketData = FXCollections.observableArrayList();
ticketData.setAll(new Ticket("1", "Fehler 1", "Meier_A", "01.02.2015", "Offen"));
ticketData.setAll(new Ticket("2", "Fehler 2", "Schmidt_W", "01.02.2015", "In Bearbeitung"));
ticketData.setAll(new Ticket("3", "Fehler 3", "Tarasov_W", "01.02.2015", "Geschlossen"));
return ticketData;
}
...
我在这个视频中说明了但是我的桌子是空的。
答案 0 :(得分:1)
我认为你只需要在initialize()而不是for循环中设置这样的东西,而ticketData.size()>0
public void initialize(){ //added after James_D comment about initialize()
clmID.setCellValueFactory(new PropertyValueFactory<>("ticketId"));
clmTicketName.setCellValueFactory(new PropertyValueFactory<>("ticketName"));
clmLastName.setCellValueFactory(new PropertyValueFactory<>("ticketLastName"));
clmCategory.setCellValueFactory(new PropertyValueFactory<>("ticketCategory"));
clmFirstName.setCellValueFactory(new PropertyValueFactory<>("ticketFirstName"));
}
编辑:
以下是演示: https://github.com/varren/How-I-can-bind-my-List-to-a-TableView-in-JavaFX/tree/master/src/sample
这是教程: https://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACFEHBI