Class TableColumn<S,T>
- Type Parameters:
-
S- The type of the TableView generic type (i.e. S == TableView<S>) -
T- The type of the content in all cells in this TableColumn.
- All Implemented Interfaces:
-
Styleable,EventTarget
public class TableColumn<S,T> extends TableColumnBase<S,T> implements EventTarget
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: - Be resized (using
minWidth/prefWidth/maxWidthandwidthproperties) - Have its
visibilitytoggled - Display
header text - Display any
nested columnsit may contain - Have a
context menuwhen the user right-clicks the column header area - Have the contents of the table be sorted (using
comparator,sortableandsortType)
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.- Since:
- JavaFX 2.0
- See Also:
Nested Class Summary
| Modifier and Type | Class | Description |
|---|---|---|
static class |
TableColumn.CellDataFeatures<S, |
A support class used in TableColumn as a wrapper class to provide all necessary information for a particular Cell. |
static class |
TableColumn.CellEditEvent<S, |
An event that is fired when a user performs an edit on a table cell. |
static enum |
TableColumn.SortType |
Enumeration that specifies the type of sorting being applied to a specific column. |
Property Summary
| Type | Property | Description |
|---|---|---|
final ObjectProperty |
cellFactory |
The cell factory for all cells in this column. |
final ObjectProperty |
cellValueFactory |
The cell value factory needs to be set to specify how to populate all cells within a single TableColumn. |
final ObjectProperty |
onEditCancel |
This event handler will be fired when the user cancels editing a cell. |
final ObjectProperty |
onEditCommit |
This event handler will be fired when the user successfully commits their editing. |
final ObjectProperty |
onEditStart |
This event handler will be fired when the user successfully initiates editing. |
final ObjectProperty |
sortType |
Used to state whether this column, if it is part of a sort order (see TableView.getSortOrder() for more details), should be sorted in ascending or descending order. |
final ReadOnlyObjectProperty |
tableView |
The TableView that this TableColumn belongs to. |
Properties declared in class javafx.scene.control.TableColumnBase
comparator, contextMenu, editable, graphic, id, maxWidth, minWidth, parentColumn, prefWidth, reorderable, resizable, sortable, sortNode, style, text, visible, width
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
static final Callback |
DEFAULT_CELL_FACTORY |
If no cellFactory is specified on a TableColumn instance, then this one will be used by default. |
Fields declared in class javafx.scene.control.TableColumnBase
DEFAULT_COMPARATOR
Constructor Summary
| Constructor | Description |
|---|---|
TableColumn() |
Creates a default TableColumn with default cell factory, comparator, and onEditCommit implementation. |
TableColumn |
Creates a TableColumn with the text set to the provided string, with default cell factory, comparator, and onEditCommit implementation. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
final ObjectProperty |
cellFactoryProperty() |
The cell factory for all cells in this column. |
final ObjectProperty |
cellValueFactoryProperty() |
The cell value factory needs to be set to specify how to populate all cells within a single TableColumn. |
static <S, |
editAnyEvent() |
Parent event for any TableColumn edit event. |
static <S, |
editCancelEvent() |
Indicates that the editing has been canceled, meaning that no change should be made to the backing data source. |
static <S, |
editCommitEvent() |
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. |
static <S, |
editStartEvent() |
Indicates that the user has performed some interaction to start an edit event, or alternatively the TableView.edit(int, javafx.scene.control.TableColumn) method has been called. |
final Callback |
getCellFactory() |
Gets the value of the cellFactory property. |
final ObservableValue |
getCellObservableValue |
Attempts to return an ObservableValue<T> for the item in the given index (which is of type S). |
final ObservableValue |
getCellObservableValue |
Attempts to return an ObservableValue<T> for the given item (which is of type S). |
final Callback |
getCellValueFactory() |
Gets the value of the cellValueFactory property. |
static List |
getClassCssMetaData() |
Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses. |
final ObservableList |
getColumns() |
This enables support for nested columns, which can be useful to group together related data. |
List |
getCssMetaData() |
The CssMetaData of this Styleable. |
final EventHandler |
getOnEditCancel() |
Gets the value of the onEditCancel property. |
final EventHandler |
getOnEditCommit() |
Gets the value of the onEditCommit property. |
final EventHandler |
getOnEditStart() |
Gets the value of the onEditStart property. |
final TableColumn.SortType |
getSortType() |
Gets the value of the sortType property. |
Styleable |
getStyleableParent() |
Return the parent of this Styleable, or null if there is no parent. |
final TableView |
getTableView() |
Gets the value of the tableView property. |
String |
getTypeSelector() |
The type of this Styleable that is to be used in selector matching. |
final ObjectProperty |
onEditCancelProperty() |
This event handler will be fired when the user cancels editing a cell. |
final ObjectProperty |
onEditCommitProperty() |
This event handler will be fired when the user successfully commits their editing. |
final ObjectProperty |
onEditStartProperty() |
This event handler will be fired when the user successfully initiates editing. |
final void |
setCellFactory |
Sets the value of the cellFactory property. |
final void |
setCellValueFactory |
Sets the value of the cellValueFactory property. |
final void |
setOnEditCancel |
Sets the value of the onEditCancel property. |
final void |
setOnEditCommit |
Sets the value of the onEditCommit property. |
final void |
setOnEditStart |
Sets the value of the onEditStart property. |
final void |
setSortType |
Sets the value of the sortType property. |
final ObjectProperty |
sortTypeProperty() |
Used to state whether this column, if it is part of a sort order (see TableView.getSortOrder() for more details), should be sorted in ascending or descending order. |
final ReadOnlyObjectProperty |
tableViewProperty() |
The TableView that this TableColumn belongs to. |
Methods declared in class javafx.scene.control.TableColumnBase
buildEventDispatchChain, comparatorProperty, contextMenuProperty, editableProperty, getCellData, getCellData, getComparator, getContextMenu, getGraphic, getId, getMaxWidth, getMinWidth, getParentColumn, getPrefWidth, getProperties, getPseudoClassStates, getSortNode, getStyle, getStyleClass, getText, getUserData, getWidth, graphicProperty, hasProperties, idProperty, isEditable, isReorderable, isResizable, isSortable, isVisible, maxWidthProperty, minWidthProperty, parentColumnProperty, prefWidthProperty, reorderableProperty, resizableProperty, setComparator, setContextMenu, setEditable, setGraphic, setId, setMaxWidth, setMinWidth, setPrefWidth, setReorderable, setResizable, setSortable, setSortNode, setStyle, setText, setUserData, setVisible, sortableProperty, sortNodeProperty, styleProperty, textProperty, visibleProperty, widthProperty
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods declared in interface javafx.event.EventTarget
addEventFilter, addEventHandler, buildEventDispatchChain, removeEventFilter, removeEventHandler
Methods declared in interface javafx.css.Styleable
getStyleableNode
Property Details
tableView
public final ReadOnlyObjectProperty<TableView<S>> tableViewProperty
- See Also:
cellValueFactory
public final ObjectProperty<Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>> cellValueFactoryProperty
Callback that provides a TableColumn.CellDataFeatures instance, and expects an ObservableValue to be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen. An example of how to set a cell value factory is:
lastNameCol.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().lastNameProperty();
}
});
}
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:
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));
- See Also:
cellFactory
public final ObjectProperty<Callback<TableColumn<S,T>,TableCell<S,T>>> cellFactoryProperty
TableCell for a single table column. By default, TableColumn uses the default cell
factory, but this can be replaced with a custom implementation, for example, to show data in a different way or to support editing. There is a lot of documentation on creating custom cell factories elsewhere (see Cell and TableView for example).
Finally, there are a number of pre-built cell factories available in the javafx.scene.control.cell package.
- See Also:
sortType
public final ObjectProperty<TableColumn.SortType> sortTypeProperty
TableView.getSortOrder() for more details), should be sorted in ascending or descending order. Simply toggling this property will result in the sort order changing in the TableView, assuming of course that this column is in the sortOrder ObservableList to begin with.- See Also:
onEditStart
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditStartProperty
- See Also:
onEditCommit
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCommitProperty
- See Also:
onEditCancel
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCancelProperty
- See Also:
Field Details
DEFAULT_CELL_FACTORY
public static final Callback<TableColumn<?,?>,TableCell<?,?>> DEFAULT_CELL_FACTORY
graphic property if the item is a Node, or it simply calls toString() if it is not null, setting the resulting string inside the text property.Constructor Details
TableColumn
public TableColumn()
TableColumn
public TableColumn(String text)
- Parameters:
-
text- The string to show when the TableColumn is placed within the TableView.
Method Details
editAnyEvent
public static <S, T> EventType<TableColumn.CellEditEvent<S,T>> editAnyEvent()
- Type Parameters:
-
S- The type of the TableView generic type -
T- The type of the content in all cells in this TableColumn - Returns:
- The any TableColumn edit event
editStartEvent
public static <S, T> EventType<TableColumn.CellEditEvent<S,T>> editStartEvent()
TableView.edit(int, javafx.scene.control.TableColumn) method has been called.- Type Parameters:
-
S- The type of the TableView generic type -
T- The type of the content in all cells in this TableColumn - Returns:
- The start an edit event
editCancelEvent
public static <S, T> EventType<TableColumn.CellEditEvent<S,T>> editCancelEvent()
- Type Parameters:
-
S- The type of the TableView generic type -
T- The type of the content in all cells in this TableColumn - Returns:
- The cancel an edit event
editCommitEvent
public static <S, T> EventType<TableColumn.CellEditEvent<S,T>> editCommitEvent()
- Type Parameters:
-
S- The type of the TableView generic type -
T- The type of the content in all cells in this TableColumn - Returns:
- The commit an edit event
tableViewProperty
public final ReadOnlyObjectProperty<TableView<S>> tableViewProperty()
- Returns:
- the
tableViewproperty - See Also:
getTableView
public final TableView<S> getTableView()
tableView property.- Property description:
- The TableView that this TableColumn belongs to.
- Returns:
- the value of the
tableViewproperty - See Also:
setCellValueFactory
public final void setCellValueFactory(Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> value)
cellValueFactory property.- Property description:
- 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
Callbackthat provides aTableColumn.CellDataFeaturesinstance, and expects anObservableValueto be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen. An example of how to set a cell value factory is:
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 thelastNameCol.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().lastNameProperty(); } }); }PropertyValueFactoryclass. 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:lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName")); - Parameters:
-
value- the value for thecellValueFactoryproperty - See Also:
getCellValueFactory
public final Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> getCellValueFactory()
cellValueFactory property.- Property description:
- 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
Callbackthat provides aTableColumn.CellDataFeaturesinstance, and expects anObservableValueto be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen. An example of how to set a cell value factory is:
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 thelastNameCol.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().lastNameProperty(); } }); }PropertyValueFactoryclass. 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:lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName")); - Returns:
- the value of the
cellValueFactoryproperty - See Also:
cellValueFactoryProperty
public final ObjectProperty<Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>> cellValueFactoryProperty()
Callback that provides a TableColumn.CellDataFeatures instance, and expects an ObservableValue to be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen. An example of how to set a cell value factory is:
lastNameCol.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().lastNameProperty();
}
});
}
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:
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));
- Returns:
- the
cellValueFactoryproperty - See Also:
setCellFactory
public final void setCellFactory(Callback<TableColumn<S,T>,TableCell<S,T>> value)
cellFactory property.- Property description:
- The cell factory for all cells in this column. The cell factory is responsible for rendering the data contained within each
TableCellfor a single table column.By default,
TableColumnuses thedefault cell factory, but this can be replaced with a custom implementation, for example, to show data in a different way or to support editing. There is a lot of documentation on creating custom cell factories elsewhere (seeCellandTableViewfor example).Finally, there are a number of pre-built cell factories available in the
javafx.scene.control.cellpackage. - Parameters:
-
value- the value for thecellFactoryproperty - See Also:
getCellFactory
public final Callback<TableColumn<S,T>,TableCell<S,T>> getCellFactory()
cellFactory property.- Property description:
- The cell factory for all cells in this column. The cell factory is responsible for rendering the data contained within each
TableCellfor a single table column.By default,
TableColumnuses thedefault cell factory, but this can be replaced with a custom implementation, for example, to show data in a different way or to support editing. There is a lot of documentation on creating custom cell factories elsewhere (seeCellandTableViewfor example).Finally, there are a number of pre-built cell factories available in the
javafx.scene.control.cellpackage. - Returns:
- the value of the
cellFactoryproperty - See Also:
cellFactoryProperty
public final ObjectProperty<Callback<TableColumn<S,T>,TableCell<S,T>>> cellFactoryProperty()
TableCell for a single table column. By default, TableColumn uses the default cell
factory, but this can be replaced with a custom implementation, for example, to show data in a different way or to support editing. There is a lot of documentation on creating custom cell factories elsewhere (see Cell and TableView for example).
Finally, there are a number of pre-built cell factories available in the javafx.scene.control.cell package.
- Returns:
- the
cellFactoryproperty - See Also:
sortTypeProperty
public final ObjectProperty<TableColumn.SortType> sortTypeProperty()
TableView.getSortOrder() for more details), should be sorted in ascending or descending order. Simply toggling this property will result in the sort order changing in the TableView, assuming of course that this column is in the sortOrder ObservableList to begin with.- Returns:
- the
sortTypeproperty - See Also:
setSortType
public final void setSortType(TableColumn.SortType value)
sortType property.- Property description:
- Used to state whether this column, if it is part of a sort order (see
TableView.getSortOrder()for more details), should be sorted in ascending or descending order. Simply toggling this property will result in the sort order changing in the TableView, assuming of course that this column is in the sortOrder ObservableList to begin with. - Parameters:
-
value- the value for thesortTypeproperty - See Also:
getSortType
public final TableColumn.SortType getSortType()
sortType property.- Property description:
- Used to state whether this column, if it is part of a sort order (see
TableView.getSortOrder()for more details), should be sorted in ascending or descending order. Simply toggling this property will result in the sort order changing in the TableView, assuming of course that this column is in the sortOrder ObservableList to begin with. - Returns:
- the value of the
sortTypeproperty - See Also:
setOnEditStart
public final void setOnEditStart(EventHandler<TableColumn.CellEditEvent<S,T>> value)
onEditStart property.- Property description:
- This event handler will be fired when the user successfully initiates editing.
- Parameters:
-
value- the value for theonEditStartproperty - See Also:
getOnEditStart
public final EventHandler<TableColumn.CellEditEvent<S,T>> getOnEditStart()
onEditStart property.- Property description:
- This event handler will be fired when the user successfully initiates editing.
- Returns:
- the value of the
onEditStartproperty - See Also:
onEditStartProperty
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditStartProperty()
- Returns:
- the on edit start property
- See Also:
setOnEditCommit
public final void setOnEditCommit(EventHandler<TableColumn.CellEditEvent<S,T>> value)
onEditCommit property.- Property description:
- This event handler will be fired when the user successfully commits their editing.
- Parameters:
-
value- the value for theonEditCommitproperty - See Also:
getOnEditCommit
public final EventHandler<TableColumn.CellEditEvent<S,T>> getOnEditCommit()
onEditCommit property.- Property description:
- This event handler will be fired when the user successfully commits their editing.
- Returns:
- the value of the
onEditCommitproperty - See Also:
onEditCommitProperty
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCommitProperty()
- Returns:
- the on edit commit property
- See Also:
setOnEditCancel
public final void setOnEditCancel(EventHandler<TableColumn.CellEditEvent<S,T>> value)
onEditCancel property.- Property description:
- This event handler will be fired when the user cancels editing a cell.
- Parameters:
-
value- the value for theonEditCancelproperty - See Also:
getOnEditCancel
public final EventHandler<TableColumn.CellEditEvent<S,T>> getOnEditCancel()
onEditCancel property.- Property description:
- This event handler will be fired when the user cancels editing a cell.
- Returns:
- the value of the
onEditCancelproperty - See Also:
onEditCancelProperty
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCancelProperty()
- Returns:
- the on edit cancel property
- See Also:
getColumns
public final ObservableList<TableColumn<S,?>> getColumns()
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.
- Specified by:
-
getColumnsin classTableColumnBase<S,T> - Returns:
- An ObservableList containing TableColumnBase instances (or subclasses) that are the children of this TableColumnBase. If these children TableColumnBase instances are set as visible, they will appear beneath this table column.
getCellObservableValue
public final ObservableValue<T> getCellObservableValue(int index)
This is achieved by calling the cell value factory, and returning whatever it returns when passed a CellDataFeatures (see, for example, the CellDataFeatures classes belonging to TableColumn and TreeTableColumn for more information).
- Specified by:
-
getCellObservableValuein classTableColumnBase<S,T> - Parameters:
-
index- The index of the item (of type S) for which an ObservableValue<T> is sought. - Returns:
- An ObservableValue<T> for this specific table column.
getCellObservableValue
public final ObservableValue<T> getCellObservableValue(S item)
This is achieved by calling the cell value factory, and returning whatever it returns when passed a CellDataFeatures (see, for example, the CellDataFeatures classes belonging to TableColumn and TreeTableColumn for more information).
- Specified by:
-
getCellObservableValuein classTableColumnBase<S,T> - Parameters:
-
item- The item (of type S) for which an ObservableValue<T> is sought. - Returns:
- An ObservableValue<T> for this specific table column.
getTypeSelector
public String getTypeSelector()
Styleable that is to be used in selector matching. This is analogous to an "element" in HTML. (CSS Type Selector).- Specified by:
-
getTypeSelectorin interfaceStyleable - Returns:
- "TableColumn"
- Since:
- JavaFX 8.0
getStyleableParent
public Styleable getStyleableParent()
- Specified by:
-
getStyleableParentin interfaceStyleable - Returns:
getTableView()- Since:
- JavaFX 8.0
getCssMetaData
public List<CssMetaData<? extends Styleable,?>> getCssMetaData()
- Specified by:
-
getCssMetaDatain interfaceStyleable - Returns:
- the CssMetaData
- Since:
- JavaFX 8.0
getClassCssMetaData
public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
CssMetaData associated with this class, which may include the CssMetaData of its superclasses.- Returns:
- the
CssMetaData - Since:
- JavaFX 8.0
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.