Java Reference
In-Depth Information
Done with the setup
State of service is RUNNING
Adding question projavafx.Question@482fb3d5
...
Adding question projavafx.Question@2d622bf7
State of service is SUCCEEDED
This shows that the
getObservableList
method returns before the questions are obtained and added to the list.
■
in theory, you could notice a different behavior inasmuch as the background thread might be completed before
the other initialization has been done. in practice, however, this behavior is unlikely when network calls are involved.
Note
Converting Web Services Data to
TableView
So far, all our examples showed questions in a
ListView
. The
ListView
is an easy and powerful JavaFX Control,
however, there are other controls that are in some cases more suitable to render information.
We can show the
Question
data in a
TableView
as well, and that is what we do in this section. The retrieval and
parsing of the data stay the same as in the previous example. However, we now use a
TableView
to render the data,
and we have to define which columns we want to see. For each column, we have to specify the origination of the data.
The code in Listing 11-15 shows the start method used in the example.
Listing 11-15.
The Start Method in the Application Rendering
Question
s in a
TableView
@Override
public void start(Stage primaryStage) throws IOException {
TableView<Question> tableView = new TableView<>();
tableView.setItems(getObservableList());
TableColumn<Question, String> dateColumn = new TableColumn<>("Date");
TableColumn<Question, String> ownerColumn = new TableColumn<>("Owner");
TableColumn<Question, String> questionColumn = new TableColumn<>("Question");
dateColumn.setCellValueFactory((CellDataFeatures<Question, String> cdf) -> {
Question q = cdf.getValue();
return new SimpleStringProperty(getTimeStampString(q.getTimestamp()));
});
ownerColumn.setCellValueFactory((CellDataFeatures<Question, String> cdf) -> {
Question q = cdf.getValue();
return new SimpleStringProperty(q.getOwner());
});
questionColumn.setCellValueFactory((CellDataFeatures<Question, String> cdf) -> {
Question q = cdf.getValue();
return new SimpleStringProperty(q.getQuestion());
});
questionColumn.setPrefWidth(350);
tableView.getColumns().addAll(dateColumn, ownerColumn, questionColumn);
StackPane root = new StackPane();
root.getChildren().add(tableView);
Scene scene = new Scene(root, 500, 300);
Search WWH ::
Custom Search