我想将JSON Rest Webservice中的元素添加到JavaFX中的TableView。除了ArrayList contains the right elements
之外,TableView只显示列名但没有行。
这是我的代码:
private Client client = ClientBuilder.newClient();
private Gson g = new Gson();
private ArrayList<Schueler> schueler = new ArrayList<Schueler>();
private ArrayList<Lehrer> lehrer = new ArrayList<Lehrer>();
private final String REST_SERVICE_URL = "http://localhost:8080/A07_Webservice/rest/ManagementService";
@FXML
private TableView<Schueler> tblSchueler;
@FXML
private TableColumn<Schueler, String> lastnameCol;
@FXML
private TableColumn<Schueler, String> idCol;
@FXML
private TableColumn<Schueler, String> firstnameCol;
@FXML
private TableColumn<Schueler, String> birthdateCol;
@FXML
private TableColumn<Schueler, String> svnrCol;
@FXML
private TableView<Lehrer> tblLehrer;
@FXML
private TableColumn<Lehrer, String> svnrLehrerCol;
@FXML
private TableColumn<Lehrer, String> firstnameLehrerCol;
@FXML
private TableColumn<Lehrer, String> lastnameLehrerCol;
@FXML
private Button btnGetSchueler;
@FXML
private Button btnGetLehrer;
@FXML
public void btnGetSchueler() {
tblSchueler.getItems().removeAll(tblSchueler.getItems());
setCellConfigurationsSchueler();
Type t = new TypeToken<List<Schueler>>() {}.getType();
String s = client.target(REST_SERVICE_URL).path("schueler").request(MediaType.APPLICATION_JSON).get(String.class);
List<Schueler> sch = g.fromJson(s, t);
schueler = (ArrayList<Schueler>) sch;
for(int i = 0; i < schueler.size(); i++) {
tblSchueler.getItems().add(schueler.get(i));
}
}
@FXML
public void btnGetLehrer() {
tblLehrer.getItems().removeAll(tblLehrer.getItems());
setCellConfigurationsLehrer();
Type t = new TypeToken<List<Lehrer>>() {}.getType();
String s = client.target(REST_SERVICE_URL).path("lehrer").request(MediaType.APPLICATION_JSON).get(String.class);
List<Lehrer> l = g.fromJson(s, t);
lehrer = (ArrayList<Lehrer>) l;
for(int i = 0; i < lehrer.size(); i++) {
tblLehrer.getItems().add(lehrer.get(i));
}
}
你知道为什么TableView是空的吗?我打印了arraylist的大小,所以我知道它包含数据。
答案 0 :(得分:0)
创建列数组并将其添加到@FXML private TableView<ObservableList<String>> tblSchueler;
//Clear
tblSchueler_data = FXCollections.observableArrayList();
tblSchueler.getItems().clear();
tblSchueler.getColumns().clear();
//Add columns
TableColumn tc[] = {
new TableColumn("Key"),
new TableColumn("Value")
};
for(int i=0; i<tc.length; i++) {
final int j = i;
tc[i].setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
Platform.runLater(() -> tblSchueler.getColumns().add(tc[j]));
}
//Add rows
ObservableList<String> row = FXCollections.observableArrayList();
Iterator entries = map.entrySet().iterator();
while(entries.hasNext()) {
Map.Entry<String,String> s = (Map.Entry<String, String>) entries.next();
row.add(s.getKey());
row.add(s.getValue());
tblSchueler_data.add(row);
}
Platform.runLater(() -> tblSchueler.setItems(tblSchueler_data));