TableRowExpanderColumn<Customer> expander = new TableRowExpanderColumn<>(param -> {
HBox editor = new HBox(10);
TextField text = new TextField(param.getValue().getName());
Button save = new Button("Save customer");
save.setOnAction(event -> {
save();
param.toggleExpanded();
});
editor.getChildren().addAll(text, save);
return editor;
});
tableView.getColumns().add(expander);
You can provide a custom cellFactory to customize the toggle button. A typical custom toggle cell implementation
would look like this:
public class MyCustomToggleCell<S> extends TableCell<S, Boolean> {
private Button button = new Button();
public MyCustomToggleCell(TableRowExpanderColumn<S> column) {
button.setOnAction(event -> column.toggleExpanded(getIndex()));
}
protected void updateItem(Boolean expanded, boolean empty) {
super.updateItem(expanded, empty);
if (expanded == null || empty) {
setGraphic(null);
} else {
button.setText(expanded ? "Collapse" : "Expand");
setGraphic(button);
}
}
}
The custom toggle cell utilizes the TableRowExpanderColumn#toggleExpanded(int) method to toggle
the row expander instead of param.toggleExpanded() like the editor does.
| Modifier and Type | Class and Description |
|---|---|
| public static class | TableRowExpanderColumn.
The type of items in the TableView S>This object is passed to the expanded node callback when it is time to create a Node to represent the expanded editor of a certain row. |
| private class | TableRowExpanderColumn.
The default toggle cell creates a button with a + or - sign as the text, depending on the expanded state of the row it represents. |
| Modifier and Type | Field and Description |
|---|---|
| private final Map | |
| private Callback | |
| private static final String | |
| private final Map | |
| private static final String |
| Access | Constructor and Description |
|---|---|
| public | TableRowExpanderColumn(Callback<TableRowExpanderColumn.
Create a row expander column that can be added to the TableView list of columns. |
| Modifier and Type | Method and Description |
|---|---|
| public Node | Returns: The expanded node, if it exists.The item corresponding to a table row item)Return the expanded node for the given item, if it exists. |
| public BooleanProperty | Returns: The boolean propertyThe item corresponding to a table row item)Returns a Boolean property that can be used to manipulate the expanded state for a row corresponding to the given item value. |
| public Node | Returns: The expanded node for the given itemThe table row, used to find the item index tableRow)Get or create and cache the expanded node for a given item. |
| private void | installRowFactoryOnTableViewAssignment()
Install the row factory on the TableView when this column is assigned to a TableView. |
| public void | toggleExpanded(int
The index of the row you want to toggle expansion for. index)Toggle the expanded state of the row at the given index. |
| expandedNodeCache | back to summary |
|---|---|
| private final Map<S, Node> expandedNodeCache | |
| expandedNodeCallback | back to summary |
|---|---|
| private Callback<TableRowExpanderColumn. | |
| EXPANDER_BUTTON_STYLE_CLASS | back to summary |
|---|---|
| private static final String EXPANDER_BUTTON_STYLE_CLASS | |
| expansionState | back to summary |
|---|---|
| private final Map<S, BooleanProperty> expansionState | |
| STYLE_CLASS | back to summary |
|---|---|
| private static final String STYLE_CLASS | |
| TableRowExpanderColumn | back to summary |
|---|---|
| public TableRowExpanderColumn(Callback<TableRowExpanderColumn. Create a row expander column that can be added to the TableView list of columns.
The expandedNodeCallback is expected to return a Node representing the editor that should appear below the
table row when the toggle button within the expander column is clicked.
Once this column is assigned to a TableView, it will automatically install a custom row factory for the TableView
so that it can configure a TableRow with the
| |
| getExpandedNode | back to summary |
|---|---|
| public Node getExpandedNode(S item) Return the expanded node for the given item, if it exists.
| |
| getExpandedProperty | back to summary |
|---|---|
| public BooleanProperty getExpandedProperty(S item) Returns a Boolean property that can be used to manipulate the expanded state for a row corresponding to the given item value.
| |
| getOrCreateExpandedNode | back to summary |
|---|---|
| public Node getOrCreateExpandedNode(TableRow<S> tableRow) Get or create and cache the expanded node for a given item. | |
| installRowFactoryOnTableViewAssignment | back to summary |
|---|---|
| private void installRowFactoryOnTableViewAssignment() Install the row factory on the TableView when this column is assigned to a TableView. | |
| toggleExpanded | back to summary |
|---|---|
| public void toggleExpanded(int index) Toggle the expanded state of the row at the given index.
| |
getValue()} which returns the
object represented by the current row.
Further more, the expandedProperty() returns a boolean property indicating the current expansion
state of the current row. You can use this, or the toggleExpanded() method to toggle and inspect
the expanded state of the row, for example if you want an action inside the row editor to contract the editor.
| Modifier and Type | Field and Description |
|---|---|
| private BooleanProperty | |
| private TableRowExpanderColumn | |
| private TableRow | |
| private S |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public BooleanProperty | Returns: The expanded propertyThe expanded property can be used to inspect or mutate the toggled state of this row editor. |
| public TableRowExpanderColumn | Returns: The TableColumn which contains the toggle buttonReturn the TableColumn which contains the toggle button. |
| public TableRow | |
| public S | Returns: The value represented by the current table rowThe value represented by the current table row. |
| public Boolean | Returns: A boolean indicating the expanded state of the current editorReturns a boolean indicating if the current row is expanded or not |
| public void | |
| public void |
| expandedProperty | back to summary |
|---|---|
| private BooleanProperty expandedProperty | |
| tableColumn | back to summary |
|---|---|
| private TableRowExpanderColumn<S> tableColumn | |
| tableRow | back to summary |
|---|---|
| private TableRow<S> tableRow | |
| value | back to summary |
|---|---|
| private S value | |
| TableRowDataFeatures | back to summary |
|---|---|
| public TableRowDataFeatures(TableRow<S> tableRow, TableRowExpanderColumn<S> tableColumn, S value) | |
| expandedProperty | back to summary |
|---|---|
| public BooleanProperty expandedProperty() The expanded property can be used to inspect or mutate the toggled state of this row editor. You can also listen for changes to it's state if needed.
| |
| getTableColumn | back to summary |
|---|---|
| public TableRowExpanderColumn Return the TableColumn which contains the toggle button. Normally you would not need to use this directly,
but rather consult the
| |
| getTableRow | back to summary |
|---|---|
| public TableRow Return the current TableRow. It is safe to assume that the index returned by
| |
| getValue | back to summary |
|---|---|
| public S getValue() The value represented by the current table row. It is important that the value has valid equals/hashCode methods, as the row value is used to keep track of the node editor for each row.
| |
| isExpanded | back to summary |
|---|---|
| public Boolean isExpanded() Returns a boolean indicating if the current row is expanded or not
| |
| setExpanded | back to summary |
|---|---|
| public void setExpanded(Boolean expanded) Set the expanded state. This will update the
| |
| toggleExpanded | back to summary |
|---|---|
| public void toggleExpanded() Toggle the expanded state of this row editor. | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | updateItem(Boolean expanded, boolean empty)
Overrides javafx. The updateItem method should not be called by developers, but it is the best method for developers to override to allow for them to customise the visuals of the cell. |
| button | back to summary |
|---|---|
| private Button button | |
| ToggleCell | back to summary |
|---|---|
| public ToggleCell() | |
| updateItem | back to summary |
|---|---|
| protected void updateItem(Boolean expanded, boolean empty) Overrides javafx. Doc from javafx. The updateItem method should not be called by developers, but it is the
best method for developers to override to allow for them to customise the
visuals of the cell. To clarify, developers should never call this method
in their code (they should leave it up to the UI control, such as the
It is very important that subclasses of Cell override the updateItem method properly, as failure to do so will lead to issues such as blank cells or cells with unexpected content appearing within them. Here is an example of how to properly override the updateItem method:
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
setText(item.toString());
}
}
Note in this code sample two important points:
| |