没有导致TableView

时间:2016-05-27 07:08:02

标签: java json javafx jackson fxml

我在 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);  
}

Marks.java

并得分:

TableView

1 个答案:

答案 0 :(得分:0)

便捷实现PropertyValueFactory无法检索“属性的属性”:即它无法从title实例的test属性中获取Marks属性。自己实施Callback

second.setCellValueFactory(cellData -> 
    new SimpleStringProperty(cellData.getValue().getTest().getTitle()));