public class TableColumn
extends java.lang.Object
implements EventTarget
A TableView is made up of a number of TableColumn instances. Each TableColumn in a table is responsible for displaying (and editing) the contents of that column. As well as being responsible for displaying and editing data for a single column, a TableColumn also contains the necessary properties to:
ObservableList data = ...
TableView tableView = new TableView(data);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setCellValueFactory(new Callback, ObservableValue>() {
public ObservableValue call(CellDataFeatures p) {
// p.getValue() returns the Person instance for a particular TableView row
return p.getValue().firstNameProperty();
}
});
tableView.getColumns().add(firstNameCol);}
firstNameCol.setCellValueFactory(new Callback, ObservableValue>() {
public ObservableValue call(CellDataFeatures p) {
return new ReadOnlyObjectWrapper(p.getValue().getFirstName());
}
});
It is hoped that over time there will be convenience cell value factories developed and made available to developers. As of the JavaFX 2.0 release, there is one such convenience class: PropertyValueFactory. This class removes the need to write the code above, instead relying on reflection to look up a given property from a String. Refer to the PropertyValueFactory class documentation for more information on how to use this with a TableColumn. Finally, for more detail on how to use TableColumn, there is further documentation in the TableView class documentation.
public final ObjectProperty
lastNameCol.setCellValueFactory(new Callback, ObservableValue>() {
public ObservableValue call(CellDataFeatures p) {
// p.getValue() returns the Person instance for a particular TableView row
return p.getValue().lastNameProperty();
}
});
}
TableColumn.CellDataFeaturesA common approach is to want to populate cells in a TableColumn using a single value from a Java bean. To support this common scenario(脚本), there is the PropertyValueFactory class. Refer to this class for more information on how to use it, but briefly here is how the above use case could be simplified using the PropertyValueFactory class: