Top Description Inners Fields Constructors Methods
org.controlsfx.control.spreadsheet

public Class SpreadsheetView

extends Control
Class Inheritance
Static Imports
impl.org.controlsfx.i18n.Localization.asKey, .Localization.localize

The SpreadsheetView is a control similar to the JavaFX TableView control but with different functionalities and use cases. The aim is to have a powerful grid where data can be written and retrieved.

Features


Freezing Rows and Columns


You can freeze some rows and some columns by right-clicking on their header. A context menu will appear if it's possible to freeze them. When frozen, the label header will then be in italic and the background will turn to dark grey.
You have also the possibility to freeze them manually by adding and removing items from getFixedRows() and getFixedColumns(). But you are strongly advised to check if it's possible to do so with SpreadsheetColumn#isColumnFixable() for the frozen columns and with isRowFixable(int) for the frozen rows.
A set of rows cannot be frozen if any cell inside these rows has a row span superior to the number of frozen rows. Likewise, a set of columns cannot be frozen if any cell inside these columns has a column span superior to the number of frozen columns.

If you want to freeze several rows or columns together, and they have a span inside, you can call areRowsFixable(java.util.List) or areSpreadsheetColumnsFixable(java.util.List) to verify if you can freeze them. Be sure to add them all in once otherwise the system will detect that a span is going out of bounds and will throw an exception. Calling those methods prior every move will ensure that no exception will be thrown.

You have also the possibility to deactivate these possibilities. For example, you force some row/column to be frozen and then the user cannot change the settings.

Headers


You can also access and toggle header's visibility by using the methods provided like setShowRowHeader(boolean) or setShowColumnHeader(boolean).
Users can double-click on a column header will resize the column to the best size in order to fully see each cell in it. Same rule apply for row header. Also note that double-clicking on the little space between two row or two columns (when resizable) will also work just like Excel.

Pickers


You can show some little images next to the headers. They will appear on the left of the VerticalHeader and on top on the HorizontalHeader. They are called "picker" because they were used originally for picking a row or a column to insert in the SpreadsheetView.
But you can do anything you want with it. Simply put a row or a column index in getRowPickers() and getColumnPickers() along with an instance of Picker. You can override the Picker#onClick() method in order to react when the user click on the picker.
The pickers will appear on the top of the column's header and on the left of the row's header.
For example, here is a picker displayed for a row that allow to group rows like Excel:
A Picker that can hide some rows.

Once we clicked on the picker (minus sign), the rows are hidden.
A Picker that can sho some rows.

Here is the code related to the images :
Picker picker = new Picker() {
         @Override
         public void onClick() {
         //If my details are hidden
             if (getHiddenRows().get(3)) {
                 showRow(3);
                 showRow(4);
                 showRow(5);
                 showRow(6);
             } else {
                 hideRow(3);
                 hideRow(4);
                 hideRow(5);
                 hideRow(6);
             }
         }
};
getRowPickers().put(2, picker);

Copy pasting

You can copy any cell you want and paste it elsewhere. Be aware that only the value inside will be pasted, not the style nor the type. Thus the value you're trying to paste must be compatible with the SpreadsheetCellType of the receiving cell. Pasting a Double into a String will work but the reverse operation will not.
See SpreadsheetCellType Value Verification documentation for more information.
A unique cell or a selection can be copied and pasted.

Hiding rows and columns

Rows and columns can be hidden if you need to. Simply call showRow(int) or hideRow(int) in order to toggle the visibility of a row. Same for the column.
Note that the span of the cell (in row or column) will automatically adapt based on the visible rows or columns. You have nothing to do.
Because toggling visibility have an impact on the Grid, if you have a lot of rows/columns to show or hide, you may consider setting them all directly by using setHiddenRows(java.util.BitSet). The BitSet represent all your rows/columns and the bit associated to it represent its visibility.

Zoom

The SpreadsheetView offers the possibility to zoom in or out. This is useful when you have a second monitor and you want your whole grid to fit in. Or when you want to draw the attention on a particular portion of the grid.
You can modify the zoom factor by playing with setZoomFactor(java.lang.Double). We recommend using value between 2 and 0.1.
Also note that the SpreadsheetView is configured to react when CTRL + and CTRL - are triggered by, respectively, zooming in and zooming out by 10%. Also CTRL 0 will bring the zoom back to default (1).

Code Samples

Just like the TableView, you instantiate the underlying model, a Grid. You will create some rows filled with SpreadsheetCell.

int rowCount = 15;
    int columnCount = 10;
    GridBase grid = new GridBase(rowCount, columnCount);

    ObservableList<ObservableList<SpreadsheetCell>> rows = FXCollections.observableArrayList();
    for (int row = 0; row < grid.getRowCount(); ++row) {
        final ObservableList<SpreadsheetCell> list = FXCollections.observableArrayList();
        for (int column = 0; column < grid.getColumnCount(); ++column) {
            list.add(SpreadsheetCellType.STRING.createCell(row, column, 1, 1,"value"));
        }
        rows.add(list);
    }
    grid.setRows(rows);

    SpreadsheetView spv = new SpreadsheetView(grid);

At that moment you can span some of the cells with the convenient method provided by the grid. Then you just need to instantiate the SpreadsheetView.

Visual:

Screenshot of SpreadsheetView
See Also
SpreadsheetCell, SpreadsheetCellBase, SpreadsheetColumn, Grid, GridBase, Picker

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SpreadsheetView.ColumnWidthEvent

This event is thrown on the SpreadsheetView when the user resize a column with its mouse.

public static class
SpreadsheetView.RowHeightEvent

This event is thrown on the SpreadsheetView when the user resize a row with its mouse.

public static enum
SpreadsheetView.SpanType

The SpanType describes in which state each cell can be.</center>

Field Summary

Modifier and TypeField and Description
private CellGraphicFactory<T extends Node>
private final SpreadsheetGridView
cellsView

Private Fields * *

private HashMap<Integer, Integer>
private final ObservableMap<Integer, Picker>
private ObservableList<SpreadsheetColumn>
private final BitSet
columnWidthSet

Since the default with applied to TableColumn is 80.

private final ChangeListener<ContextMenu>
private static final double
DEFAULT_ROW_HEADER_WIDTH

Default width of the VerticalHeader.

private Map<SpreadsheetCellType<?>, SpreadsheetCellEditor>
private FilteredList<ObservableList<SpreadsheetCell>>
private Integer
private final ObservableList<SpreadsheetColumn>
private final ListChangeListener<SpreadsheetColumn>
private final ObservableList<Integer>
private final ListChangeListener<Integer>
fixedRowsListener

********************************************************************* * private listeners ********************************************************************

private final BooleanProperty
private final BooleanProperty
private DataFormat
private SimpleObjectProperty<Grid>
pack-priv final SpreadsheetHandle
private final ObjectProperty<BitSet>
private final ObjectProperty<BitSet>
private final EventHandler<WindowEvent>
private IdentityHashMap<ObservableList<SpreadsheetCell>, Integer>
private final EventHandler<KeyEvent>
private static final double
private static final double
private BitSet
private final DoubleProperty
rowHeaderWidth

The vertical header width, just for the Label, not the Pickers.

private HashMap<Integer, Integer>
private final ObservableMap<Integer, Picker>
private final SpreadsheetViewSelectionModel
private final BooleanProperty
private final BooleanProperty
private SortedList<ObservableList<SpreadsheetCell>>
private static final double
private String
private DoubleProperty
private final KeyCombination
private final KeyCombination
private final KeyCombination
private final KeyCombination
private final KeyCombination
private final KeyCombination
private final KeyCombination
private final KeyCombination

Constructor Summary

AccessConstructor and Description
public
SpreadsheetView()

This constructor will generate sample Grid with 100 rows and 15 columns.

public
SpreadsheetView(final Grid
The Grid that contains the items to be rendered
grid
)

Creates a SpreadsheetView control with the Grid specified.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the List of columns can be frozen together.
areColumnsFixable
(List<? extends Integer> list)

This method is the same as areSpreadsheetColumnsFixable(java.util.List) but is using a List of SpreadsheetColumn indexes.

public boolean

Returns:

true if the List of row can be frozen together.
areRowsFixable
(List<? extends Integer> list)

Indicates whether a List of rows can be frozen or not.

public boolean

Returns:

true if the List of columns can be frozen together.
areSpreadsheetColumnsFixable
(List<? extends SpreadsheetColumn> list)

Indicates whether a List of SpreadsheetColumn can be fixed or not.

private void
pack-priv void
columnWidthSet(int indexColumn)

Used by SpreadsheetColumn internally in order to specify if a column width has been set by the user.

public ObjectProperty<Comparator<? super ObservableList<SpreadsheetCell>>>

Returns:

an ObjectProperty wrapping the comparator used in the SpreadsheetView.
comparatorProperty
()

Return an ObjectProperty wrapping the comparator used in the SpreadsheetView.

private void
private String
computeReason(List<? extends Integer> list)

private void
public void
copyClipboard()

Put the current selection into the ClipBoard.

public void
decrementZoom()

Decrement the level of zoom by 0.10.

public void
deleteSelectedCells()

This method is called when pressing the "delete" key on the SpreadsheetView.

public void
edit(int row, SpreadsheetColumn column)

Causes the cell at the given row/column view indexes to switch into its editing state, if it is not already in it, and assuming that the SpreadsheetView and column are also editable.

public final BooleanProperty

Returns:

the BooleanProperty associated with the editableProperty.
editableProperty
()

Specifies whether this SpreadsheetView is editable - only if the SpreadsheetView, and the SpreadsheetCell within it are both editable will a SpreadsheetCell be able to go into its editing state.

public ReadOnlyObjectProperty<TablePosition<ObservableList<SpreadsheetCell>, ?>>

Returns:

the current cell being edited, or null if there is no cell being edited.
editingCellProperty
()

Represents the current cell being edited, or null if there is no cell being edited.

public ReadOnlyBooleanProperty

Returns:

the Boolean property associated with the allowance of freezing or unfreezing some columns.
fixingColumnsAllowedProperty
()

Return the Boolean property associated with the allowance of freezing or unfreezing some columns.

public ReadOnlyBooleanProperty

Returns:

the Boolean property associated with the allowance of freezing or unfreezing some rows.
fixingRowsAllowedProperty
()

Return the Boolean property associated with the allowance of freezing or unfreezing some rows.

public CellGraphicFactory<T extends Node>

Returns:

the CellGraphicFactory
getCellGraphicFactory
()

Returns the CellGraphicFactory if set that provide implementation for browser in SpreadsheetCell.

pack-priv final SpreadsheetGridView
getCellsView()

Returns: the inner table view

pack-priv final GridViewSkin
getCellsViewSkin()

Returns: the inner table view skin

public ObservableMap<Integer, Picker>
getColumnPickers()

Returns: An ObservableMap with the column index as key and the Picker as a value.

public final ObservableList<SpreadsheetColumn>

Returns:

An ObservableList of the SpreadsheetColumn
getColumns
()

Return an ObservableList of the SpreadsheetColumn used.

public int

Returns:

the current column span of a Cell.
getColumnSpan
(SpreadsheetCell cell)

Return the current column span of a Cell considering all hidden columns.

public Comparator<T>

Returns:

the comparator used in the SortedList for the SpreadsheetView.
getComparator
()

Return the comparator used in the SortedList for the SpreadsheetView.

public TablePosition<ObservableList<SpreadsheetCell>, ?>

Returns:

a TablePosition of cell being currently edited.
getEditingCell
()

Return a TablePosition of cell being currently edited.

public final Optional<SpreadsheetCellEditor>

Returns:

the editor associated with the CellType.
getEditor
(SpreadsheetCellType<?> cellType)

Return the editor associated with the CellType.

public int

Returns:

the row where the Filter will be shown.
getFilteredRow
()

Return the row where the Filter will be shown.

public int

Returns:

the index used in the SpreadsheetView.
getFilteredRow
(int modelRow)

Given a row index base on the Grid, return the index used in the SpreadsheetView.

public int

Returns:

the model row
getFilteredSourceIndex
(int viewRow)

Given an index on the SpreadsheetView, it will return the model row by simply considering the hidden rows (and not the actual sort if any).

public ObservableList<SpreadsheetColumn>

Returns:

an ObservableList of the frozen columns.
getFixedColumns
()

You can freeze or unfreeze a column by modifying this list.

public ObservableList<Integer>

Returns:

an ObservableList of integer representing the frozen rows.
getFixedRows
()

You can freeze or unfreeze a row by modifying this list.

public final Grid

Returns:

the model Grid used by the SpreadsheetView
getGrid
()

Return the model Grid used by the SpreadsheetView

public double

Returns:

the value of the horizontal scrollbar.
getHBarValue
()

Return the value of the horizontal scrollbar.

public BitSet

Returns:

a BitSet of the Hidden columns, where true means the column is hidden.
getHiddenColumns
()

Return a BitSet of the Hidden columns, where true means the column is hidden.

public BitSet

Returns:

a BitSet of the Hidden rows, where true means the row is hidden.
getHiddenRows
()

Return a BitSet of the Hidden rows, where true means the row is hidden.

public ObservableList<ObservableList<SpreadsheetCell>>

Returns:

the current list of rows.
getItems
()

Return the current list of rows set in the SpreadsheetView as they appear on the screen.

public int

Returns:

an index based on the getColumns() list of the SpreadsheetView.
getModelColumn
(int viewColumn)

Given a column index based on the visible column list, for example when dealing with TablePosition#getColumn().

public int

Returns:

a Grid index it is related to.
getModelRow
(int
a row index based on the SpreadsheetView
viewRow
)

Given an index on the SpreadsheetView, return a Grid index it is related to.

public final Node

Returns:

the Node used as a placeholder that is shown when the SpreadsheetView has no content to show.
getPlaceholder
()

Gets the value of the placeholder property.

public int

Returns:

the current reverse row span for the given cell
getReverseRowSpan
(SpreadsheetCell
the considered SpreadsheetCell
cell
,
int
the current row position of this cell
index
)

Return the exact opposite of getRowSpan(org.controlsfx.control.spreadsheet.SpreadsheetCell, int).

public final double
getRowHeaderWidth()

Returns: the current width of the row header.

public double
getRowHeight(int row)

Returns: the height of a particular row of the SpreadsheetView.

public ObservableMap<Integer, Picker>
getRowPickers()

Returns: An ObservableMap with the row index as key and the Picker as a value.

public int

Returns:

the current row span for the given cell
getRowSpan
(SpreadsheetCell
the considered SpreadsheetCell
cell
,
int
the current row position of this cell
index
)

Return the current row span for the given cell at the given position in the Table.

public int

Returns:

the row span for the given cell.
getRowSpanFilter
(SpreadsheetCell cell)

Return the row span for the given cell without considering the actual sort.

private static Grid

Returns:

the sample Grid
getSampleGrid
()

This static method creates a sample Grid with 100 rows and 15 columns.

public SpreadsheetViewSelectionModel
getSelectionModel()

Return the selectionModel used by the SpreadsheetView.

public SpreadsheetView.SpanType

Returns:

The SpanType of a cell
getSpanType
(final int rowIndex, final int modelColumn)

Return the SpanType of a cell.

public ContextMenu

Returns:

the ContextMenu to use.
getSpreadsheetViewContextMenu
()

Create a menu on rightClick with two options: Copy/Paste This can be overridden by developers for custom behavior.

private TableColumn<ObservableList<SpreadsheetCell>, SpreadsheetCell>
getTableColumn(Grid grid, int columnIndex)

This is called when setting a Grid.

public String
getUserAgentStylesheet()

Overrides javafx.scene.layout.Region.getUserAgentStylesheet.

An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method.

public double

Returns:

the value of the vertical scrollbar.
getVBarValue
()

Return the value of the vertical scrollbar.

public int

Returns:

an index based on the visible columns in the SpreadsheetView.
getViewColumn
(int modelColumn)

Given a column index based on the getColumns() list, return an index based on the visible columns in the SpreadsheetView.

private int
getViewIndex(int sourceIndex)

public int

Returns:

the ViewRow if possible, -1 or another row if the row is hidden.
getViewRow
(int
the row retrieved in SpreadsheetCell#getRow()
modelRow
)

Given the row of a SpreadsheetCell, returns the actual row as displayed in the SpreadsheetView.

public final Double

Returns:

the zoomFactor used for the SpreadsheetView.
getZoomFactor
()

Return the zoomFactor used for the SpreadsheetView.

public final ReadOnlyObjectProperty<Grid>
gridProperty()

Return a ReadOnlyObjectProperty containing the current Grid used in the SpreadsheetView.

public final ObjectProperty<BitSet>

Returns:

the Objectproperty wrapping the hidden columns.
hiddenColumnsProperty
()

Return the Objectproperty wrapping the hidden columns.

public final ObjectProperty<BitSet>

Returns:

the Objectproperty wrapping the hidden rows..
hiddenRowsProperty
()

Return the Objectproperty wrapping the hidden rows..

public void
hideColumn(SpreadsheetColumn column)

Hide the specified SpreadsheetColumn.

public void
hideRow(int row)

Hide the specified row.

public void
incrementZoom()

Increment the level of zoom by 0.10.

private void
public boolean

Returns:

true if the column if freezable
isColumnFixable
(int columnIndex)

Indicate whether this column can be frozen or not.

public boolean

Returns:

true if this column index (regarding to getColumns() is hidden.
isColumnHidden
(int column)

Return true if this column index (regarding to getColumns() is hidden.

public final boolean

Returns:

a boolean telling if the SpreadsheetView is editable.
isEditable
()

Gets the value of the property editable.

private boolean
isEditionKey(KeyEvent keyEvent)

We want NOT to go in edition if we're pressing SHIFT and if we're using the navigation keys.

public boolean

Returns:

whether change to frozen columns are allowed.
isFixingColumnsAllowed
()

Return whether change to frozen columns are allowed.

public boolean

Returns:

whether change to frozen rows are allowed.
isFixingRowsAllowed
()

Return whether change to frozen rows are allowed.

public boolean

Returns:

true if the row can be frozen.
isRowFixable
(int row)

Indicate whether a row can be frozen or not.

public boolean

Returns:

true is this row is hidden.
isRowHidden
(int row)

Return true is this row is hidden.

public final boolean

Returns:

a boolean telling whether the column Header is shown
isShowColumnHeader
()

Return if the Column Header is showing.

public final boolean

Returns:

a boolean telling if the row Header is being shown
isShowRowHeader
()

Return if the row Header is showing.

protected void
public void
pasteClipboard()

Try to paste the clipBoard to the specified position.

private void
pasteMixedValues(ArrayList<ClipboardCell> list)

Try to paste the values given into the selection.

private void
pasteOneValue(ClipboardCell change)

Paste one value from the clipboard over the whole selection.

private void
pasteSeveralValues(ArrayList<ClipboardCell> list)

If we have several source values to paste into one cell, we do it.

public final ObjectProperty<Node>
placeholderProperty()

This Node is shown to the user when the SpreadsheetView has no content to show.

public void
resizeRowsToDefault()

This method will wipe all changes made to the row's height and set all row's height back to their default height defined in the model Grid.

public void
resizeRowsToFitContent()

This method will compute the best height for each line.

public void
resizeRowsToMaximum()

This method will first apply resizeRowsToFitContent() and then take the highest height and apply it to every row.\n Just as resizeRowsToFitContent(), this method can be degrading your performance on great grid.

public final DoubleProperty

Returns:

A DoubleProperty.
rowHeaderWidthProperty
()

This DoubleProperty represents the width of the rowHeader.

public void
scrollToColumn(SpreadsheetColumn column)

Scrolls the SpreadsheetView so that the given SpreadsheetColumn is visible.

public void
scrollToColumnIndex(int modelColumn)

Scrolls the SpreadsheetView so that the given column index is visible.

public void
scrollToRow(int
the row to scroll to
row
)

Scrolls the SpreadsheetView so that the given row is visible.

public void
setCellGraphicFactory(CellGraphicFactory<T extends Node>
the CellGraphicFactory
cellGraphicFactory
)

Sets the CellGraphicFactory that will provide an implementation for cell that have SpreadsheetCell#isCellGraphic() set to true.

public void
setComparator(Comparator<ObservableList<SpreadsheetCell>>
the comparator that will sort the rows.
comparator
)

Sets a new Comparator for the SpreadsheetView in order to sort the rows.

public final void
setEditable(final boolean b)

Sets the value of the property editable.

public void
setFilteredRow(Integer row)

Set the row (based of Grid indexes) where the Filter will appear.

public void
setFixingColumnsAllowed(boolean b)

If set to true, user will be allowed to freeze and unfreeze the columns.

public void
setFixingRowsAllowed(boolean b)

If set to true, user will be allowed to freeze and unfreeze the rows.

public final void
setGrid(Grid
the new Grid
grid
)

Set a new Grid for the SpreadsheetView.

public void
setHBarValue(double value)

Same method as ScrollBar#setValue(double) on the verticalBar.

private void
setHBarValue(double value, int attempt)

public void
setHiddenColumns(BitSet hiddenColumns)

Give a complete new BitSet of the hidden columns.

public void
setHiddenRows(BitSet hiddenRows)

Give a complete new BitSet of the hidden rows.

public final void
setPlaceholder(final Node
the node to show when the SpreadsheetView has no content to show.
placeholder
)

Sets the value of the placeholder property

public final void
setRowHeaderWidth(double value)

Specify a new width for the row header.

public final void
setShowColumnHeader(final boolean b)

Activate and deactivate the Column Header

public final void
setShowRowHeader(final boolean b)

Activate and deactivate the Row Header.

public void
setVBarValue(double value)

Same method as ScrollBar#setValue(double) on the verticalBar.

public final void
setZoomFactor(Double zoomFactor)

Set a new zoomFactor for the SpreadsheetView.

public void
showColumn(SpreadsheetColumn column)

Show the specified SpreadsheetColumn.

public final BooleanProperty

Returns:

the BooleanProperty associated with the column Header.
showColumnHeaderProperty
()

BooleanProperty associated with the column Header.

public void
showRow(int row)

Show the specified row.

public final BooleanProperty

Returns:

the BooleanProperty associated with the row Header.
showRowHeaderProperty
()

BooleanProperty associated with the row Header.

private void
tryPasteCell(int row, int column, ClipboardCell change)

Try to paste the given value into the given position.

private void
private void
verifyGrid(Grid grid)

Verify that the grid is well-formed.

public final DoubleProperty

Returns:

the zoomFactor used for the SpreadsheetView.
zoomFactorProperty
()

Return the zoomFactor used for the SpreadsheetView.

Inherited from javafx.scene.control.Control:
computeMaxHeightcomputeMaxWidthcomputeMinHeightcomputeMinWidthcomputePrefHeightcomputePrefWidthcontextMenuPropertycreateDefaultSkinexecuteAccessibleActiongetBaselineOffsetgetClassCssMetaDatagetContextMenugetControlCssMetaDatagetCssMetaDatagetInitialFocusTraversablegetSkingetTooltipisResizablequeryAccessibleAttributesetContextMenusetSkinsetTooltipskinPropertytooltipProperty