Java Reference
In-Depth Information
Assigning Items to a Table
The
TableView
constructor at the beginning of Listing 6-6 causes the
ObservableList
containing
Person
instances
(returned from the
getTeamMembers()
method) to be associated with the
TableView
. If the contents of the underlying
ObservableList
change, the
TableView
is automatically updated to reflect the changes.
Defining
TableView
Columns
To define the
columns
in our
TableView
we use the methods shown in this snippet from Listing 6-6:
TableColumn firstNameColumn = new TableColumn("First Name");
firstNameColumn.setCellValueFactory(new PropertyValueFactory("firstName"));
firstNameColumn.setPrefWidth(180);
The
String parameter we provide to the constructor
specifies the text that should appear in the column
header, and the
setPrefWidth()
method specifies the column's preferred width in pixels.
The argument passed into the
setCellValueFactory()
method specifies a property that will be used to populate
this column. In this case, the property is the
firstNameProperty
defined in the
Person
model class of our StarterApp
program, shown in Listing 6-7.
Listing 6-7.
The Source Code for
Person.java
package projavafx.starterapp.model;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public final class Person {
private StringProperty firstName;
public void setFirstName(String value) { firstNameProperty().set(value); }
public String getFirstName() { return firstNameProperty().get(); }
public StringProperty firstNameProperty() {
if (firstName == null) firstName = new SimpleStringProperty(this, "firstName");
return firstName;
}
private StringProperty lastName;
public void setLastName(String value) { lastNameProperty().set(value); }
public String getLastName() { return lastNameProperty().get(); }
public StringProperty lastNameProperty() {
if (lastName == null) lastName = new SimpleStringProperty(this, "lastName");
return lastName;
}
private StringProperty phone;
public void setPhone(String value) { phoneProperty().set(value); }
public String getPhone() { return phoneProperty().get(); }
public StringProperty phoneProperty() {
if (phone == null) phone = new SimpleStringProperty(this, "phone");
return phone;
}
Search WWH ::
Custom Search