Java Reference
In-Depth Information
In addition to supplying text and content , we're also specifying that the tab shouldn't be closable ,
and that some processing should occur when the user chooses the tab. The latter is implemented using the
onSelectionChanged() method shown previously, which enables you to implement life cycle functionality when a
tab is exposed or hidden (i.e., selected or not selected). In the preceding snippet, we're causing the WebView (which is
covered later) to load a randomly selected site when the tab is selected.
Now that you understand how the menus, toolbar, and tabs were created in the StarterApp program, let's
examine the UI controls on each tab. We start with the leftmost tab, labeled TableView, and work our way to the right.
Creating a TableView
As you experienced in Steps 7 and 8 of the exercise, the TableView shown in Figure 6-1 contains 10,000 rows of data,
and allows its columns to be rearranged and resized. The code that defines and populates the TableView in the
StarterApp program is shown in Listing 6-6.
Listing 6-6. The createTableDemoNode() Method Located in StarterAppMain.java
Node createTableDemoNode() {
TableView table = new TableView(model.getTeamMembers());
TableColumn firstNameColumn = new TableColumn("First Name");
firstNameColumn.setCellValueFactory(new PropertyValueFactory("firstName"));
firstNameColumn.setPrefWidth(180);
TableColumn lastNameColumn = new TableColumn("Last Name");
lastNameColumn.setCellValueFactory(new PropertyValueFactory("lastName"));
lastNameColumn.setPrefWidth(180);
TableColumn phoneColumn = new TableColumn("Phone Number");
phoneColumn.setCellValueFactory(new PropertyValueFactory("phone"));
phoneColumn.setPrefWidth(180);
table.getColumns().addAll(firstNameColumn, lastNameColumn, phoneColumn);
table.getSelectionModel().selectedItemProperty()
.addListener((ObservableValue observable, Object oldValue, Object newValue) -> {
Person selectedPerson = (Person) newValue;
System.out.println(selectedPerson + " chosen in TableView");
});
return table;
}
In addition to the code in Listing 6-6, the following code snippet from Listing 6-1 contains a method from our
StarterAppModel class that creates the Person instances that will be displayed in the TableView :
public ObservableList getTeamMembers() {
ObservableList teamMembers = FXCollections.observableArrayList();
for (int i = 1; i <= 10000; i++) {
teamMembers.add(new Person("FirstName" + i,
"LastName" + i,
"Phone" + i));
}
return teamMembers;
}
 
Search WWH ::




Custom Search