- java.lang.Object
-
- org.controlsfx.control.spreadsheet.GridBase
-
- All Implemented Interfaces:
EventTarget
,Grid
public class GridBase extends Object implements Grid, EventTarget
A base implementation of theGrid
interface.How to span
First of all, the Grid must have all its rows filled with the same number of
SpreadsheetCell
. A span is materialized by the same cell (same instance of SpreadsheetCell) repeated all over the covered part. In order to materialize span, you have two ways :- First way is to manually add the same cell where you want to span. For example, we will make the first cell span on two columns :
//I create a sample grid 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); } //I create my SpreadsheetCell spanning on two columns. SpreadsheetCell cell = SpreadsheetCellType.STRING.createCell(0, 0, 1, 2,"value"); //I add them in the area covered by the span. rows.get(0).set(0, cell); rows.get(0).set(1, cell); grid.setRows(rows); SpreadsheetView spv = new SpreadsheetView(grid);
- The second way is to build the SpreadsheetView, but to use thespanRow(int, int, int)
orspanColumn(int, int, int)
methods. These methods will take the SpreadsheetCell at the specified position, and enlarge the span by modifying the rowSpan or columnSpan of the cell. And also put the SpreadsheetCell in the area covered by the span.//I create a sample grid 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); } //I First set the rows in the grid. grid.setRows(rows); //Then I simply tell the grid to span the first cell grid.spanColumn(2,0,0); SpreadsheetView spv = new SpreadsheetView(grid);
Row Height
You can specify some row height for some of your rows at the beginning. You have to use the methodsetRowHeightCallback(javafx.util.Callback)
in order to specify a Callback that will give you the index of the row, and you will give back the height of the row.
If you just have aMap
available, you can use theGridBase.MapBasedRowHeightFactory
that will construct the Callback for you. The default height is 24.0.Cell values
If you want to change the value of a cell, you have to go through the API with
setCellValue(int, int, Object)
. This method will verify that the value is corresponding to theSpreadsheetCellType
of the cell and try to convert it if possible. It will also fire aGridChange
event in order to notify all listeners that a value has changed.
If you want to listen to those changes, you can use the
addEventHandler(EventType, EventHandler)
andremoveEventHandler(EventType, EventHandler)
methods.
A basic listener for implementing a undo/redo in the SpreadsheetView could be like that:Grid grid = ...; Stack<GridChange> undoStack = ...; grid.addEventHandler(GridChange.GRID_CHANGE_EVENT, new EventHandler<GridChange>() { public void handle(GridChange change) { undoStack.push(change); } });
Display selection
By default, the SpreadsheetView will display a black rectangle around your selection if it's contiguous. Some may want to disable that effect. Therefore a simple call tosetDisplaySelection(boolean)
with a false value will make that rectangle disappear.Headers
The SpreadsheetView is displaying row numbers and column letters by default. Just like any other spreadsheet would do. However, some may want to customize theose headers. You can use thegetColumnHeaders()
andgetRowHeaders()
in order to customize what will appear in these headers.
If you put some long text in the row headers, it will not fit. Thus you may consider usingSpreadsheetView.setRowHeaderWidth(double)
in order to enlarge the row header so that your text can fit properly.- See Also:
Grid
,GridChange
-
-
Property Summary
Properties Type Property Description BooleanProperty
displaySelection
Returns the Boolean property associated with the displayed selection of theGrid
.BooleanProperty
locked
Returns a BooleanProperty associated with the locked grid state.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GridBase.MapBasedRowHeightFactory
This class serves as a bridge between row height Callback needed by the GridBase and a Map<Integer,Double> that one could have (each Integer specify a row index and its associated height).
-
Constructor Summary
Constructors Constructor Description GridBase(int rowCount, int columnCount)
Creates aGridBase
with a fixed number of rows and columns.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <E extends GridChange>
voidaddEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Registers an event handler to this Grid.EventDispatchChain
buildEventDispatchChain(EventDispatchChain tail)
BooleanProperty
displaySelectionProperty()
Returns the Boolean property associated with the displayed selection of theGrid
.int
getColumnCount()
Returns how many columns are inside theGrid
.ObservableList<String>
getColumnHeaders()
Returns anObservableList
ofString
to display in the column headers.int
getRowCount()
Returns how many rows are inside theGrid
.ObservableList<String>
getRowHeaders()
Returns anObservableList
ofString
to display in the row headers.double
getRowHeight(int row)
Returns the height of a row.ObservableList<ObservableList<SpreadsheetCell>>
getRows()
boolean
isCellDisplaySelection(int row, int column)
Returns true if the given cell will display a selection rectangle when selected.boolean
isDisplaySelection()
Returntrue
if the selection (black rectangle) is displayed on theGrid
.boolean
isLocked()
Returns whether thisGridBase
id locked or not.boolean
isRowResizable(int row)
Returns true if the specified row is resizable.BooleanProperty
lockedProperty()
Returns a BooleanProperty associated with the locked grid state.<E extends GridChange>
voidremoveEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Unregisters a previously registered event handler from this Grid.void
setCellDisplaySelection(int row, int column, boolean displaySelection)
Overrides the value defined byGrid.isDisplaySelection()
so that no matter what is defined on the grid, the given cell will always have its selection set to the displaySelection parameter.void
setCellValue(int modelRow, int column, Object value)
Changes the value situated at the intersection if possible.void
setDisplaySelection(boolean value)
If set to true, the selection (black rectangle) will be displayed on theGrid
.void
setLocked(Boolean lock)
Locks or unlocks thisGridBase
.void
setResizableRows(BitSet resizableRow)
Sets the resizable state of all rows.void
setRowHeightCallback(Callback<Integer,Double> rowHeight)
Sets a newCallback
for this grid in order to specify height of each row.void
setRows(Collection<ObservableList<SpreadsheetCell>> rows)
Sets the rows used by the grid, and updates the rowCount.void
spanColumn(int count, int rowIndex, int colIndex)
Spans in column the cell situated at rowIndex and colIndex by the number count.void
spanRow(int count, int rowIndex, int colIndex)
Spans in row the cell situated at rowIndex and colIndex by the number count.
-
-
-
Property Detail
-
locked
public BooleanProperty lockedProperty
Returns a BooleanProperty associated with the locked grid state. It means that the Grid is in a read-only mode and that no SpreadsheetCell can be modified, no regards for their ownSpreadsheetCell.isEditable()
state.- See Also:
isLocked()
,setLocked(Boolean)
-
displaySelection
public BooleanProperty displaySelectionProperty
Returns the Boolean property associated with the displayed selection of theGrid
.- Specified by:
displaySelectionProperty
in interfaceGrid
- See Also:
isDisplaySelection()
,setDisplaySelection(boolean)
-
-
Method Detail
-
getRows
public ObservableList<ObservableList<SpreadsheetCell>> getRows()
Returns anObservableList
ofObservableList
ofSpreadsheetCell
instances. Refer to theGrid
class javadoc for more detail.- Specified by:
getRows
in interfaceGrid
- Returns:
- an
ObservableList
ofObservableList
ofSpreadsheetCell
instances
-
setCellValue
public void setCellValue(int modelRow, int column, Object value)
Changes the value situated at the intersection if possible. Verification and conversion of the value should be done before withSpreadsheetCellType.match(Object)
andSpreadsheetCellType.convertValue(Object)
.- Specified by:
setCellValue
in interfaceGrid
- Parameters:
modelRow
- the row index issued from theSpreadsheetCell
column
- the column index issued from theSpreadsheetCell
value
- the value to set to theSpreadsheetCell
-
getRowCount
public int getRowCount()
Returns how many rows are inside theGrid
.- Specified by:
getRowCount
in interfaceGrid
- Returns:
- the number of rows in the
Grid
.
-
getColumnCount
public int getColumnCount()
Returns how many columns are inside theGrid
.- Specified by:
getColumnCount
in interfaceGrid
- Returns:
- the number of columns in the
Grid
.
-
getRowHeight
public double getRowHeight(int row)
Returns the height of a row.Grid.AUTOFIT
can be returned in order to let the system compute the best row height.- Specified by:
getRowHeight
in interfaceGrid
- Parameters:
row
- the row index- Returns:
- the height in pixels of the given row.
-
setRowHeightCallback
public void setRowHeightCallback(Callback<Integer,Double> rowHeight)
Sets a newCallback
for this grid in order to specify height of each row.- Parameters:
rowHeight
- theCallback
to use for rown height computation
-
getRowHeaders
public ObservableList<String> getRowHeaders()
Returns anObservableList
ofString
to display in the row headers.- Specified by:
getRowHeaders
in interfaceGrid
- Returns:
- an
ObservableList
ofString
to display in the row headers
-
getColumnHeaders
public ObservableList<String> getColumnHeaders()
Returns anObservableList
ofString
to display in the column headers.- Specified by:
getColumnHeaders
in interfaceGrid
- Returns:
- an
ObservableList
ofString
to display in the column headers
-
lockedProperty
public BooleanProperty lockedProperty()
Returns a BooleanProperty associated with the locked grid state. It means that the Grid is in a read-only mode and that no SpreadsheetCell can be modified, no regards for their ownSpreadsheetCell.isEditable()
state.- See Also:
isLocked()
,setLocked(Boolean)
-
isLocked
public boolean isLocked()
Returns whether thisGridBase
id locked or not.- Returns:
true
if thisGridBase
is locked
-
setLocked
public void setLocked(Boolean lock)
Locks or unlocks thisGridBase
.- Parameters:
lock
-true
to lock thisGridBase
-
spanRow
public void spanRow(int count, int rowIndex, int colIndex)
Spans in row the cell situated at rowIndex and colIndex by the number count.
-
spanColumn
public void spanColumn(int count, int rowIndex, int colIndex)
Spans in column the cell situated at rowIndex and colIndex by the number count.- Specified by:
spanColumn
in interfaceGrid
- Parameters:
count
- the span rangerowIndex
- the row indexcolIndex
- the column index
-
setRows
public void setRows(Collection<ObservableList<SpreadsheetCell>> rows)
Sets the rows used by the grid, and updates the rowCount. This method should be called before theGrid
is actually given to aSpreadsheetView
. If this method is called after, you should give theGrid
again to theSpreadsheetView
by usingSpreadsheetView.setGrid(org.controlsfx.control.spreadsheet.Grid)
.
-
setResizableRows
public void setResizableRows(BitSet resizableRow)
Sets the resizable state of all rows. If a bit is set to true in theBitSet
, it means the row is resizable. TheBitSet.length()
must be equal to thegetRowCount()
- Parameters:
resizableRow
- aBitSet
where the bits set totrue
represent the resizable rows
-
isRowResizable
public boolean isRowResizable(int row)
Returns true if the specified row is resizable.- Specified by:
isRowResizable
in interfaceGrid
- Parameters:
row
- the row index- Returns:
true
if the specified row is resizable
-
isDisplaySelection
public boolean isDisplaySelection()
Returntrue
if the selection (black rectangle) is displayed on theGrid
. Cells may override this property withGrid.setCellDisplaySelection(int, int, boolean)
.- Specified by:
isDisplaySelection
in interfaceGrid
- Returns:
true
if the selection (black rectangle) is displayed on the Grid
-
setDisplaySelection
public void setDisplaySelection(boolean value)
If set to true, the selection (black rectangle) will be displayed on theGrid
. Cells may override this property withGrid.setCellDisplaySelection(int, int, boolean)
.- Specified by:
setDisplaySelection
in interfaceGrid
- Parameters:
value
-true
if the selection should be displayed
-
displaySelectionProperty
public BooleanProperty displaySelectionProperty()
Returns the Boolean property associated with the displayed selection of theGrid
.- Specified by:
displaySelectionProperty
in interfaceGrid
- See Also:
isDisplaySelection()
,setDisplaySelection(boolean)
-
setCellDisplaySelection
public void setCellDisplaySelection(int row, int column, boolean displaySelection)
Overrides the value defined byGrid.isDisplaySelection()
so that no matter what is defined on the grid, the given cell will always have its selection set to the displaySelection parameter.- Specified by:
setCellDisplaySelection
in interfaceGrid
- Parameters:
row
- the row indexcolumn
- the column indexdisplaySelection
-true
is the selection should always be displayed on this cell
-
isCellDisplaySelection
public boolean isCellDisplaySelection(int row, int column)
Returns true if the given cell will display a selection rectangle when selected. If nothing is defined for this cell,Grid.isDisplaySelection()
is returned.- Specified by:
isCellDisplaySelection
in interfaceGrid
- Parameters:
row
- the row indexcolumn
- the column index- Returns:
true
if the given cell will display a selection rectangle
-
addEventHandler
public <E extends GridChange> void addEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Registers an event handler to this Grid. The Grid class allows registration of listeners which will be notified as aSpreadsheetCell
's value will change.- Specified by:
addEventHandler
in interfaceGrid
- Parameters:
eventType
- the type of the events to receive by the handlereventHandler
- the handler to register
-
removeEventHandler
public <E extends GridChange> void removeEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Unregisters a previously registered event handler from this Grid. One handler might have been registered for different event types, so the caller needs to specify the particular event type from which to unregister the handler.- Specified by:
removeEventHandler
in interfaceGrid
- Parameters:
eventType
- the event type from which to unregistereventHandler
- the handler to unregister
-
buildEventDispatchChain
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
- Specified by:
buildEventDispatchChain
in interfaceEventTarget
-
-