Java Reference
In-Depth Information
Figure 11-7. Using a TableView for rendering questions
This sample requires lots of boilerplate code for a simple table, but fortunately the JavaFX Platform contains a
way to reduce the amount of code. Manually setting the CellValueFactory instances for each column is cumbersome,
but we can use another method for doing this, by using JavaFX Properties. Listing 11-16 contains a modified version of
the start method of the main class, where we leverage the JavaFX Properties concept.
Listing 11-16. Rendering Data in Columns Based on JavaFX Properties
@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(new PropertyValueFactory<>("timestampString"));
ownerColumn.setCellValueFactory(new PropertyValueFactory<>("owner"));
questionColumn.setCellValueFactory(new PropertyValueFactory<>("question"));
questionColumn.setPrefWidth(350);
tableView.getColumns().addAll(dateColumn, ownerColumn, questionColumn);
StackPane root = new StackPane();
root.getChildren().add(tableView);
Scene scene = new Scene(root, 500, 300);
primaryStage.setTitle("StackOverflow Table");
primaryStage.setScene(scene);
primaryStage.show();
}
 
Search WWH ::




Custom Search