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.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:
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 |
---|---|
static class |
TableColumn.CellDataFeatures<S,T>
A support class used in TableColumn as a wrapper class
to provide all necessary information for a particular
Cell . |
static class |
TableColumn.CellEditEvent<S,T>
An event that is fired when a user performs an edit on a table cell.
|
static class |
TableColumn.SortType
Enumeration that specifies the type of sorting being applied to a specific
column.
|
Modifier and Type | Field and Description |
---|---|
private ObjectProperty<Callback<TableColumn<S,T>,TableCell<S,T>>> |
cellFactory
The cell factory for all cells in this column.
|
private ObjectProperty<Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>> |
cellValueFactory
The cell value factory needs to be set to specify how to populate all
cells within a single TableColumn.
|
private ObservableList<TableColumn<S,?>> |
columns
*
Instance Variables *
*
|
private ListChangeListener<TableColumn<S,?>> |
columnsListener |
static Callback<TableColumn<?,?>,TableCell<?,?>> |
DEFAULT_CELL_FACTORY
If no cellFactory is specified on a TableColumn instance, then this one
will be used by default.
|
private EventHandler<TableColumn.CellEditEvent<S,T>> |
DEFAULT_EDIT_COMMIT_HANDLER
*
Listeners *
*
|
private static java.lang.String |
DEFAULT_STYLE_CLASS
*
Stylesheet Handling *
*
|
private static EventType<?> |
EDIT_ANY_EVENT |
private static EventType<?> |
EDIT_CANCEL_EVENT |
private static EventType<?> |
EDIT_COMMIT_EVENT |
private static EventType<?> |
EDIT_START_EVENT |
private ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> |
onEditCancel |
private ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> |
onEditCommit |
private ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> |
onEditStart |
private ObjectProperty<TableColumn.SortType> |
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. |
private ReadOnlyObjectWrapper<TableView<S>> |
tableView
The TableView that this TableColumn belongs to.
|
private WeakListChangeListener<TableColumn<S,?>> |
weakColumnsListener |
DEFAULT_COMPARATOR, DEFAULT_MAX_WIDTH, DEFAULT_MIN_WIDTH, DEFAULT_WIDTH, eventHandlerManager
Constructor and Description |
---|
TableColumn()
Creates a default TableColumn with default cell factory, comparator, and
onEditCommit implementation.
|
TableColumn(java.lang.String 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 |
---|---|
ObjectProperty<Callback<TableColumn<S,T>,TableCell<S,T>>> |
cellFactoryProperty() |
ObjectProperty<Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>> |
cellValueFactoryProperty() |
static <S,T> EventType<TableColumn.CellEditEvent<S,T>> |
editAnyEvent()
Parent event for any TableColumn edit event.
|
static <S,T> EventType<TableColumn.CellEditEvent<S,T>> |
editCancelEvent()
Indicates that the editing has been canceled, meaning that no change should
be made to the backing data source.
|
static <S,T> EventType<TableColumn.CellEditEvent<S,T>> |
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,T> EventType<TableColumn.CellEditEvent<S,T>> |
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. |
Callback<TableColumn<S,T>,TableCell<S,T>> |
getCellFactory() |
ObservableValue<T> |
getCellObservableValue(int index)
Attempts to return an ObservableValue<T> for the item in the given
index (which is of type S).
|
ObservableValue<T> |
getCellObservableValue(S item)
Attempts to return an ObservableValue<T> for the given item (which
is of type S).
|
Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> |
getCellValueFactory() |
static java.util.List<CssMetaData<? extends Styleable,?>> |
getClassCssMetaData() |
ObservableList<TableColumn<S,?>> |
getColumns()
This enables support for nested columns, which can be useful to group
together related data.
|
java.util.List<CssMetaData<? extends Styleable,?>> |
getCssMetaData()
The CssMetaData of this Styleable.
|
EventHandler<TableColumn.CellEditEvent<S,T>> |
getOnEditCancel() |
EventHandler<TableColumn.CellEditEvent<S,T>> |
getOnEditCommit() |
EventHandler<TableColumn.CellEditEvent<S,T>> |
getOnEditStart() |
TableColumn.SortType |
getSortType() |
Styleable |
getStyleableParent()
Return the parent of this Styleable, or null if there is no parent.
|
TableView<S> |
getTableView() |
java.lang.String |
getTypeSelector()
The type of this
Styleable that is to be used in selector matching. |
Node |
impl_styleableGetNode()
Deprecated.
This is an internal API that is not intended for use and will be removed in the next version
|
ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> |
onEditCancelProperty()
This event handler will be fired when the user cancels editing a cell.
|
ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> |
onEditCommitProperty()
This event handler will be fired when the user successfully commits their
editing.
|
ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> |
onEditStartProperty()
This event handler will be fired when the user successfully initiates
editing.
|
private TableColumnHeader |
scan(TableColumnHeader header) |
void |
setCellFactory(Callback<TableColumn<S,T>,TableCell<S,T>> value) |
void |
setCellValueFactory(Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> value) |
void |
setOnEditCancel(EventHandler<TableColumn.CellEditEvent<S,T>> value) |
void |
setOnEditCommit(EventHandler<TableColumn.CellEditEvent<S,T>> value) |
void |
setOnEditStart(EventHandler<TableColumn.CellEditEvent<S,T>> value) |
void |
setSortType(TableColumn.SortType value) |
(package private) void |
setTableView(TableView<S> value) |
ObjectProperty<TableColumn.SortType> |
sortTypeProperty() |
ReadOnlyObjectProperty<TableView<S>> |
tableViewProperty() |
addEventHandler, 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, impl_fixedProperty, impl_isFixed, impl_isReorderable, impl_reorderableProperty, impl_setFixed, impl_setReorderable, impl_setWidth, isEditable, isResizable, isSortable, isVisible, maxWidthProperty, minWidthProperty, parentColumnProperty, prefWidthProperty, removeEventHandler, resizableProperty, setComparator, setContextMenu, setEditable, setGraphic, setId, setMaxWidth, setMinWidth, setParentColumn, setPrefWidth, setResizable, setSortable, setSortNode, setStyle, setText, setUserData, setVisible, setWidth, sortableProperty, sortNodeProperty, styleProperty, textProperty, updateColumnWidths, visibleProperty, widthProperty
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
buildEventDispatchChain
private static final EventType<?> EDIT_ANY_EVENT
private static final EventType<?> EDIT_START_EVENT
private static final EventType<?> EDIT_CANCEL_EVENT
private static final EventType<?> EDIT_COMMIT_EVENT
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.private EventHandler<TableColumn.CellEditEvent<S,T>> DEFAULT_EDIT_COMMIT_HANDLER
private ListChangeListener<TableColumn<S,?>> columnsListener
private WeakListChangeListener<TableColumn<S,?>> weakColumnsListener
private final ObservableList<TableColumn<S,?>> columns
private ReadOnlyObjectWrapper<TableView<S>> tableView
private ObjectProperty<Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>> cellValueFactory
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"));
PropertyValueFactory
private final ObjectProperty<Callback<TableColumn<S,T>,TableCell<S,T>>> cellFactory
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.
private ObjectProperty<TableColumn.SortType> sortType
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.private ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditStart
private ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCommit
private ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCancel
private static final java.lang.String DEFAULT_STYLE_CLASS
public TableColumn()
public TableColumn(java.lang.String text)
text
- The string to show when the TableColumn is placed within the TableView.public static <S,T> EventType<TableColumn.CellEditEvent<S,T>> editAnyEvent()
public static <S,T> EventType<TableColumn.CellEditEvent<S,T>> editStartEvent()
TableView.edit(int, javafx.scene.control.TableColumn)
method has been called.public static <S,T> EventType<TableColumn.CellEditEvent<S,T>> editCancelEvent()
public static <S,T> EventType<TableColumn.CellEditEvent<S,T>> editCommitEvent()
public final ReadOnlyObjectProperty<TableView<S>> tableViewProperty()
public final void setCellValueFactory(Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> value)
public final Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> getCellValueFactory()
public final ObjectProperty<Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>> cellValueFactoryProperty()
public final ObjectProperty<Callback<TableColumn<S,T>,TableCell<S,T>>> cellFactoryProperty()
public final ObjectProperty<TableColumn.SortType> sortTypeProperty()
public final void setSortType(TableColumn.SortType value)
public final TableColumn.SortType getSortType()
public final void setOnEditStart(EventHandler<TableColumn.CellEditEvent<S,T>> value)
public final EventHandler<TableColumn.CellEditEvent<S,T>> getOnEditStart()
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditStartProperty()
public final void setOnEditCommit(EventHandler<TableColumn.CellEditEvent<S,T>> value)
public final EventHandler<TableColumn.CellEditEvent<S,T>> getOnEditCommit()
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCommitProperty()
public final void setOnEditCancel(EventHandler<TableColumn.CellEditEvent<S,T>> value)
public final EventHandler<TableColumn.CellEditEvent<S,T>> getOnEditCancel()
public final ObjectProperty<EventHandler<TableColumn.CellEditEvent<S,T>>> onEditCancelProperty()
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.
getColumns
in class TableColumnBase<S,T>
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).
getCellObservableValue
in class TableColumnBase<S,T>
index
- The index of the item (of type S) for which an
ObservableValue<T> is sought.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).
getCellObservableValue
in class TableColumnBase<S,T>
item
- The item (of type S) for which an ObservableValue<T> is
sought.public java.lang.String getTypeSelector()
Styleable
that is to be used in selector matching.
This is analogous to an "element" in HTML.
(CSS Type Selector).getTypeSelector
in interface Styleable
public Styleable getStyleableParent()
getStyleableParent
in interface Styleable
getTableView()
public java.util.List<CssMetaData<? extends Styleable,?>> getCssMetaData()
getCssMetaData
in interface Styleable
public static java.util.List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
@Deprecated public Node impl_styleableGetNode()
private TableColumnHeader scan(TableColumnHeader header)