Java Reference
In-Depth Information
Figure 11-3. The result of the first example
The resulting window contains a ListView with two entries. Those entries correspond to the two questions that
are created in the getObservableList() method at the bottom of Listing 11-2.
The information about the questions that is shown in the window is not very useful. Indeed, we told the ListView
that it should display some instances of Question , but we did not tell how those should be displayed. The latter can be
achieved by specifying a CellFactory . In this chapter, our goal is not to create a fancy UI; rather, we want to show how
to retrieve data and render these data in the UI. Hence, we briefly show how the developer can alter the visualization
of data by using the CellFactory concept. For an overview of the UI Controls that we use in our examples ( ListView
and TableView ), refer to Chapter 6.
In Listing 11-3, we create a QuestionCell class that extends ListCell and defines how to lay out a cell.
Listing 11-3. Define QuestionCell
package projavafx;
import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.scene.control.ListCell;
public class QuestionCell extends ListCell<Question> {
static final SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-YY");
@Override
protected void updateItem(Question question, boolean empty){
super.updateItem(question, empty);
if (empty) {
setText("");
} else {
StringBuilder sb= new StringBuilder();
 
Search WWH ::




Custom Search