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:
minWidth/prefWidth/maxWidth
and width properties)
visibility toggled
header text
nested columns it may contain
context menu when the user
right-clicks the column header area
comparator, sortable and
sortType)
text (what to show in the column
header area), and the column cell value factory
(which is used to populate individual cells in the column). This can be
achieved using some variation on the following code:
ObservableList<Person> data = ...
TableView<Person> tableView = new TableView<Person>(data);
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
// p.getValue() returns the Person instance for a particular TableView row
return p.getValue().firstNameProperty();
}
});
tableView.getColumns().add(firstNameCol);}
This approach assumes that the object returned from p.getValue()
has a JavaFX ObservableValue that can simply be returned. The benefit of this
is that the TableView will internally create bindings to ensure that,
should the returned ObservableValue change, the cell contents will be
automatically refreshed.
In situations where a TableColumn must interact with classes created before
JavaFX, or that generally do not wish to use JavaFX apis for properties, it is
possible to wrap the returned value in a ReadOnlyObjectWrapper instance. For
example:
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Person, String> 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.
TableView, TableCell, TablePosition
| Modifier and Type | Class and Description |
|---|---|
| public static class | TableColumn.
The TableView type S, The TableColumn type T>A support class used in TableColumn as a wrapper class
to provide all necessary information for a particular |
| public static class | TableColumn.
The type of the TableView generic type S, The type of the content in all cells in this TableColumn T>An event that is fired when a user performs an edit on a table cell. |
| public static enum | TableColumn.
Enumeration that specifies the type of sorting being applied to a specific column. |
| Modifier and Type | Field and Description |
|---|---|
| private final ObjectProperty | cellFactory
The cell factory for all cells in this column. |
| private ObjectProperty | cellValueFactory
The cell value factory needs to be set to specify how to populate all cells within a single TableColumn. |
| private final ObservableList | |
| private ListChangeListener | |
| public static final Callback | DEFAULT_CELL_FACTORY
If no cellFactory is specified on a TableColumn instance, then this one will be used by default. |
| private EventHandler | |
| private static final String | |
| private static final EventType | |
| private static final EventType | |
| private static final EventType | |
| private static final EventType | |
| private ObjectProperty | |
| private ObjectProperty | |
| private ObjectProperty | |
| private ObjectProperty | sortType
Used to state whether this column, if it is part of a sort order (see
|
| private ReadOnlyObjectWrapper | tableView
The TableView that this TableColumn belongs to. |
| private WeakListChangeListener |
| Access | Constructor and Description |
|---|---|
| public | TableColumn()
Creates a default TableColumn with default cell factory, comparator, and onEditCommit implementation. |
| public | TableColumn(String
The string to show when the TableColumn is placed within the TableView. text)Creates a TableColumn with the text set to the provided string, with default cell factory, comparator, and onEditCommit implementation. |
| Modifier and Type | Method and Description |
|---|---|
| public final ObjectProperty | |
| public final ObjectProperty | |
| public static < The type of the TableView generic type S, The type of the content in all cells in this TableColumn T> EventType | |
| public static < The type of the TableView generic type S, The type of the content in all cells in this TableColumn T> EventType | Returns: The cancel an edit eventIndicates that the editing has been canceled, meaning that no change should be made to the backing data source. |
| public static < The type of the TableView generic type S, The type of the content in all cells in this TableColumn T> EventType | Returns: The commit an edit eventIndicates that the editing has been committed by the user, meaning that a change should be made to the backing data source to reflect the new data. |
| public static < The type of the TableView generic type S, The type of the content in all cells in this TableColumn T> EventType | Returns: The start an edit eventIndicates that the user has performed some interaction to start an edit
event, or alternatively the |
| public final Callback | |
| public final ObservableValue | getCellObservableValue(int index)
Implements abstract javafx. Attempts to return an ObservableValue<T> for the item in the given index (which is of type S). |
| public final ObservableValue | getCellObservableValue(S item)
Implements abstract javafx. Attempts to return an ObservableValue<T> for the given item (which is of type S). |
| public final Callback | |
| public static List | Returns: theCssMetaDataGets the |
| public final ObservableList | getColumns()
Implements abstract javafx. This enables support for nested columns, which can be useful to group together related data. |
| public List | |
| public final EventHandler | |
| public final EventHandler | |
| public final EventHandler | |
| public final TableColumn. | |
| public Node | getStyleableNode()
Overrides default javafx. Returns the Node that represents this Styleable object. |
| public Styleable | Returns: getTableView()Implements javafx. Return the parent of this Styleable, or null if there is no parent. |
| public final TableView | |
| public String | Returns: "TableColumn"Implements javafx. The type of this |
| public final ObjectProperty | Returns: the on edit cancel propertyThis event handler will be fired when the user cancels editing a cell. |
| public final ObjectProperty | Returns: the on edit commit propertyThis event handler will be fired when the user successfully commits their editing. |
| public final ObjectProperty | Returns: the on edit start propertyThis event handler will be fired when the user successfully initiates editing. |
| private TableColumnHeader | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| pack-priv final void | |
| public final ObjectProperty | |
| public final ReadOnlyObjectProperty |
| cellFactory | back to summary |
|---|---|
| private final ObjectProperty<Callback<TableColumn<S, T>, TableCell<S, T>>> cellFactory The cell factory for all cells in this column. The cell factory
is responsible for rendering the data contained within each By default, Finally, there are a number of pre-built cell factories available in the
| |
| cellValueFactory | back to summary |
|---|---|
| private ObjectProperty<Callback<TableColumn. The cell value factory needs to be set to specify how to populate all
cells within a single TableColumn. A cell value factory is a
A 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:
| |
| columns | back to summary |
|---|---|
| private final ObservableList<TableColumn<S, ?>> columns | |
| columnsListener | back to summary |
|---|---|
| private ListChangeListener<TableColumn<S, ?>> columnsListener | |
| DEFAULT_CELL_FACTORY | back to summary |
|---|---|
| public static final Callback<TableColumn<?, ?>, TableCell<?, ?>> DEFAULT_CELL_FACTORY If no cellFactory is specified on a TableColumn instance, then this one
will be used by default. At present it simply renders the TableCell item
property within the | |
| DEFAULT_EDIT_COMMIT_HANDLER | back to summary |
|---|---|
| private EventHandler<TableColumn. | |
| DEFAULT_STYLE_CLASS | back to summary |
|---|---|
| private static final String DEFAULT_STYLE_CLASS | |
| EDIT_ANY_EVENT | back to summary |
|---|---|
| private static final EventType<?> EDIT_ANY_EVENT | |
| EDIT_CANCEL_EVENT | back to summary |
|---|---|
| private static final EventType<?> EDIT_CANCEL_EVENT | |
| EDIT_COMMIT_EVENT | back to summary |
|---|---|
| private static final EventType<?> EDIT_COMMIT_EVENT | |
| EDIT_START_EVENT | back to summary |
|---|---|
| private static final EventType<?> EDIT_START_EVENT | |
| onEditCancel | back to summary |
|---|---|
| private ObjectProperty<EventHandler<TableColumn. | |
| onEditCommit | back to summary |
|---|---|
| private ObjectProperty<EventHandler<TableColumn. | |
| onEditStart | back to summary |
|---|---|
| private ObjectProperty<EventHandler<TableColumn. | |
| sortType | back to summary |
|---|---|
| private ObjectProperty<TableColumn. Used to state whether this column, if it is part of a sort order (see
| |
| tableView | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<TableView<S>> tableView The TableView that this TableColumn belongs to. | |
| weakColumnsListener | back to summary |
|---|---|
| private WeakListChangeListener<TableColumn<S, ?>> weakColumnsListener | |
| TableColumn | back to summary |
|---|---|
| public TableColumn() Creates a default TableColumn with default cell factory, comparator, and onEditCommit implementation. | |
| TableColumn | back to summary |
|---|---|
| public TableColumn(String text) Creates a TableColumn with the text set to the provided string, with default cell factory, comparator, and onEditCommit implementation.
| |
| cellFactoryProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| cellValueFactoryProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| editAnyEvent | back to summary |
|---|---|
| public static <S, T> EventType Parent event for any TableColumn edit event.
| |
| editCancelEvent | back to summary |
|---|---|
| public static <S, T> EventType Indicates that the editing has been canceled, meaning that no change should be made to the backing data source.
| |
| editCommitEvent | back to summary |
|---|---|
| public static <S, T> EventType Indicates that the editing has been committed by the user, meaning that a change should be made to the backing data source to reflect the new data.
| |
| editStartEvent | back to summary |
|---|---|
| public static <S, T> EventType Indicates that the user has performed some interaction to start an edit
event, or alternatively the
| |
| getCellFactory | back to summary |
|---|---|
| public final Callback | |
| getCellObservableValue | back to summary |
|---|---|
| public final ObservableValue Implements abstract javafx. Doc from javafx. Attempts to return an ObservableValue<T> for the item in the given index (which is of type S). In other words, this method expects to receive an integer value that is greater than or equal to zero, and less than the size of the underlying data model. If the index is valid, this method will return an ObservableValue<T> for this specific column. This is achieved by calling the
| |
| getCellObservableValue | back to summary |
|---|---|
| public final ObservableValue Implements abstract javafx. Doc from javafx. Attempts to return an ObservableValue<T> for the given item (which is of type S). In other words, this method expects to receive an object from the underlying data model for the entire 'row' in the table, and it must return an ObservableValue<T> for the value in this specific column. This is achieved by calling the
| |
| getCellValueFactory | back to summary |
|---|---|
| public final Callback | |
| getClassCssMetaData | back to summary |
|---|---|
| public static List Gets the
| |
| getColumns | back to summary |
|---|---|
| public final ObservableList Implements abstract javafx. Doc from javafx. This enables support for nested columns, which can be useful to group together related data. For example, we may have a 'Name' column with two nested columns for 'First' and 'Last' names. This has no impact on the table as such - all column indices point to the leaf columns only, and it isn't possible to sort using the parent column, just the leaf columns. In other words, this is purely a visual feature.
| |
| getCssMetaData | back to summary |
|---|---|
| public List Implements javafx. Doc from javafx. The CssMetaData of this Styleable. This may be returned as an unmodifiable list.
| |
| getOnEditCancel | back to summary |
|---|---|
| public final EventHandler | |
| getOnEditCommit | back to summary |
|---|---|
| public final EventHandler | |
| getOnEditStart | back to summary |
|---|---|
| public final EventHandler | |
| getSortType | back to summary |
|---|---|
| public final TableColumn. | |
| getStyleableNode | back to summary |
|---|---|
| public Node getStyleableNode() Overrides default javafx. Doc from javafx. Returns the Node that represents this Styleable object. This method should be overridden in cases where the Styleable is not itself a Node, so that it may optionally return the relevant root node representation of itself. By default this method returns null, which can mean that either the Styleable itself is a Node, or if that is not the case, that the Styleable does not have a node representation available at the time of request.
| |
| getStyleableParent | back to summary |
|---|---|
| public Styleable getStyleableParent() Implements javafx. Doc from javafx. Return the parent of this Styleable, or null if there is no parent. | |
| getTableView | back to summary |
|---|---|
| public final TableView | |
| getTypeSelector | back to summary |
|---|---|
| public String getTypeSelector() Implements javafx. Doc from javafx. The type of this | |
| onEditCancelProperty | back to summary |
|---|---|
| public final ObjectProperty This event handler will be fired when the user cancels editing a cell.
| |
| onEditCommitProperty | back to summary |
|---|---|
| public final ObjectProperty This event handler will be fired when the user successfully commits their editing.
| |
| onEditStartProperty | back to summary |
|---|---|
| public final ObjectProperty This event handler will be fired when the user successfully initiates editing.
| |
| scan | back to summary |
|---|---|
| private TableColumnHeader scan(TableColumnHeader header) | |
| setCellFactory | back to summary |
|---|---|
| public final void setCellFactory(Callback<TableColumn<S, T>, TableCell<S, T>> value) | |
| setCellValueFactory | back to summary |
|---|---|
| public final void setCellValueFactory(Callback<TableColumn. | |
| setOnEditCancel | back to summary |
|---|---|
| public final void setOnEditCancel(EventHandler<TableColumn. | |
| setOnEditCommit | back to summary |
|---|---|
| public final void setOnEditCommit(EventHandler<TableColumn. | |
| setOnEditStart | back to summary |
|---|---|
| public final void setOnEditStart(EventHandler<TableColumn. | |
| setSortType | back to summary |
|---|---|
| public final void setSortType(TableColumn. | |
| setTableView | back to summary |
|---|---|
| pack-priv final void setTableView(TableView<S> value) | |
| sortTypeProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| tableViewProperty | back to summary |
|---|---|
| public final ReadOnlyObjectProperty | |
Cell. Once
instantiated, this class is immutable.
| Modifier and Type | Field and Description |
|---|---|
| private final TableColumn | |
| private final TableView | |
| private final S |
| Access | Constructor and Description |
|---|---|
| public | CellDataFeatures(TableView<S>
The TableView that this instance refers to. tableView, TableColumn<S, T> The TableColumn that this instance refers to. tableColumn, S The value for a row in the TableView. value)Instantiates a CellDataFeatures instance with the given properties set as read-only values of this instance. |
| Modifier and Type | Method and Description |
|---|---|
| public TableColumn | Returns: the TableColumn passed in to the constructorReturns the |
| public TableView | Returns: the TableView passed in to the constructorReturns the |
| public S | Returns: the value passed in to the constructorReturns the value passed in to the constructor. |
| tableColumn | back to summary |
|---|---|
| private final TableColumn<S, T> tableColumn | |
| tableView | back to summary |
|---|---|
| private final TableView<S> tableView | |
| value | back to summary |
|---|---|
| private final S value | |
| CellDataFeatures | back to summary |
|---|---|
| public CellDataFeatures(TableView<S> tableView, TableColumn<S, T> tableColumn, S value) Instantiates a CellDataFeatures instance with the given properties set as read-only values of this instance.
| |
| getTableColumn | back to summary |
|---|---|
| public TableColumn Returns the
| |
| getTableView | back to summary |
|---|---|
| public TableView Returns the
| |
| getValue | back to summary |
|---|---|
| public S getValue() Returns the value passed in to the constructor.
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final EventType | ANY
Hides javafx. |
| private final T | |
| private final transient TablePosition | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public | CellEditEvent(TableView<S>
The TableView on which this event occurred. table, TablePosition<S, T> The position upon which this event occurred. pos, EventType<TableColumn.The type of event that occurred. eventType,The value input by the end user. newValue)Creates a new event that can be subsequently fired to the relevant listeners. |
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: An Object representing the new value input by the user.Returns the new value input by the end user. |
| public T | Returns: Returns the value stored in the position being edited, or null if it can not be retrieved.Attempts to return the old value at the position referred to in the
TablePosition returned by |
| public S | Returns: the value for the rowConvenience method that returns the value for the row (that is, from
the TableView |
| public TableColumn | Returns: The TableColumn that the edit occurred in.Returns the TableColumn upon which this event occurred. |
| public TablePosition | Returns: The position upon which this event occurred.Returns the position upon which this event occurred. |
| public TableView | Returns: The TableView control upon which this event occurred.Returns the TableView upon which this event occurred. |
| ANY | back to summary |
|---|---|
| public static final EventType<?> ANY Hides javafx. Common supertype for all cell edit event types.
| |
| newValue | back to summary |
|---|---|
private final T newValue
| |
| pos | back to summary |
|---|---|
| private final transient TablePosition<S, T> pos | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| CellEditEvent | back to summary |
|---|---|
| public CellEditEvent(TableView<S> table, TablePosition<S, T> pos, EventType<TableColumn. Creates a new event that can be subsequently fired to the relevant listeners.
| |
| getNewValue | back to summary |
|---|---|
| public T getNewValue() Returns the new value input by the end user. This is not the value to go back into the TableView.items list - this new value represents just the input for a single cell, so it is likely that it needs to go back into a property within an item in the TableView.items list.
| |
| getOldValue | back to summary |
|---|---|
| public T getOldValue() Attempts to return the old value at the position referred to in the
TablePosition returned by
| |
| getRowValue | back to summary |
|---|---|
| public S getRowValue() Convenience method that returns the value for the row (that is, from
the TableView
| |
| getTableColumn | back to summary |
|---|---|
| public TableColumn Returns the TableColumn upon which this event occurred.
| |
| getTablePosition | back to summary |
|---|---|
| public TablePosition Returns the position upon which this event occurred.
| |
| getTableView | back to summary |
|---|---|
| public TableView Returns the TableView upon which this event occurred.
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final TableColumn. | ASCENDING
Column will be sorted in an ascending order. |
| public static final TableColumn. | DESCENDING
Column will be sorted in a descending order. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static TableColumn. | |
| public static TableColumn. |
| ASCENDING | back to summary |
|---|---|
| public static final TableColumn. Column will be sorted in an ascending order. | |
| DESCENDING | back to summary |
|---|---|
| public static final TableColumn. Column will be sorted in a descending order. | |
| SortType | back to summary |
|---|---|
| private SortType() | |
| valueOf | back to summary |
|---|---|
| public static TableColumn. | |
| values | back to summary |
|---|---|
| public static TableColumn. | |