我在 TableView 中显示一列时遇到问题。我用JSON获取数据。 查询:
[{"idmarks":3,"idusers":1,"mark":8.0,"test":{"date":"2016-01-01T12:34:59+01:00","idtests":2,"title":"test 2"}},{"idmarks":4,"idusers":1,"mark":4.0,"test":{"date":"2014-01-01T11:11:11+01:00","idtests":3,"title":"test 111"}}]
代码:
@FXML
private void action(){
ObjectMapper mapper = new ObjectMapper();
List<Marks> lista = null;
String path = "http://localhost:8080/Server/source/users/1/marks";
try {
lista = mapper.readValue(new URL(path), new TypeReference<List<Marks>>(){});
} catch (IOException ex) {
System.out.println(ex);
}
ObservableList<Marks> lis = FXCollections.observableArrayList(lista);
table.setItems(lis);
TableColumn<Marks, String> first = new TableColumn<Marks,String>("Mark");
first.setCellValueFactory(new PropertyValueFactory("mark"));
TableColumn<Marks,String> second = new TableColumn<Marks,String>("Title");
second.setCellValueFactory(new PropertyValueFactory("title"));
table.getColumns().setAll(first, second);
}
并得分:
答案 0 :(得分:0)
便捷实现PropertyValueFactory
无法检索“属性的属性”:即它无法从title
实例的test
属性中获取Marks
属性。自己实施Callback
:
second.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().getTest().getTitle()));