Java Reference
In-Depth Information
sb.append("[").append(sdf.format(new Date(question.getTimestamp()))).append("]")
.append(" ").append(question.getOwner()+": "+question.getQuestion());
setText(sb.toString());
}
}
}
When a cell item has to be updated, we tell it to show some text containing the timestamp between square
brackets, followed by the author and the title of the question. Next, the ListView needs to be told that it should render
QuestionCell s. We do this by calling the ListView.setCellFactory() method and provide a lambda expression that
creates a new QuestionCell when called. In Listing 11-4, we show the modified version of the start method of our
StackOverflowApplication .
Listing 11-4. Use CellFactory on the ListView
public void start(Stage primaryStage) {
ListView<Question> listView = new ListView<>();
listView.setItems(getObservableList());
listView.setCellFactory(l -> new QuestionCell());
StackPane root = new StackPane();
root.getChildren().add(listView);
Scene scene = new Scene(root, 500, 300);
primaryStage.setTitle("StackOverflow List");
primaryStage.setScene(scene);
primaryStage.show();
}
If we now run the application, the output appears as in Figure 11-4 .
Figure 11-4. The result of adding a QuestionCell
 
Search WWH ::




Custom Search