wolacinio wolacinio - 6 months ago 22
Java Question

No result in TableView

I have problem with show one column in TableView. I get data with JSON.
Query:

[{"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"}}]


Code:

@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);
}


and score:

TableView

Answer

The problem with your code is

 TableColumn<Marks, String> first = new TableColumn<Marks,String>("Mark");
    first.setCellValueFactory(new PropertyValueFactory("mark"));

You should update it with :-

 TableColumn<Marks, String> first = new TableColumn<Marks,String>("Mark");
    first.setCellValueFactory(new PropertyValueFactory<Marks,String>("mark"));

Update the same for second column and add the values in table after you set the columns so use table.setItems(lis); after table.getColumns().setAll(first, second);

That will solve your problem.

Cheers