Class Sheet
- All Implemented Interfaces:
TableModel
One sheet of a Workbook: a grid of cells with no fixed size, each empty or holding a
number, text, a logical value, an error or a formula.
Two ways to address a cell
The methods named ...Cell... address the sheet the way a formula does: row 0, column 0
is A1, whatever the sheet is used for. The TableModel methods
address the data: when setHeaderRow(boolean) is on, the first row of the sheet is
the column names and model row 0 is the row under it.
Values
A cell's value is a Double, a String, a Boolean, a CellError, a java.util.Date
for a number whose format shows a date, or null for an empty cell. The value of a formula
is what it calculates to; getCellFormula(int, int) answers the formula itself.
A String passed to setValueAt(int, int, Object) is read as if it had been typed into
a spreadsheet: =A1*2 is a formula, 12.5 a number, 45% a number shown as a
percentage, 2024-03-01 a date, TRUE a logical value, and '12 the text 12.
setCellValue(int, int, Object) stores what it is given, so a String stays text.
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener that is told when a cell changes: the cell that was edited, and every formula that reads it, directly or through other formulas.voidaddMergedRegion(int firstRow, int firstColumn, int lastRow, int lastColumn) Merges a rectangle of cells into one, as the file formats record it.voidAdds a row under the last one.Describes the columns of the sheet for aDataGrid: the kind of each -- number, date, logical, text -- from the values it holds, a formatter that shows values in the number format most of the column's cells have, and the width the file gave the column.getCellFormat(int row, int column) The number format of a cell.getCellFormula(int row, int column) The formula of a cell.getCellText(int row, int column) The text a cell shows: its value in its number format.getCellValue(int row, int column) The value of a cell.getCellValue(String address) The value of a cell named the way a formula names it.intReturns the number of columns in the tablegetColumnName(int i) The name of a column: the text of the header row's cell when the sheet has a header row and the cell is not empty, the letters of the column otherwise.doublegetColumnWidth(int column) The width of a column.getFormulaAt(int row, int column) The formula of a cell of the table model.intThe number of columns at the left that stay in place while the rest scrolls.intThe number of rows at the top that stay in place while the rest scrolls.int[]getMergedRegion(int index) A merged region.intThe number of merged regions.getName()The name of the sheet, which formulas on other sheets refer to it by.intReturns the number of rows in the tableintgetRowOutlineLevel(int row) The outline level of a row: how deep it is nested in the groups a spreadsheet can collapse.getValueAt(int row, int column) Returns the value of the cell at the given locationThe workbook the sheet is part of.voidinsertColumns(int column, int count) Inserts empty columns.voidinsertRows(int row, int count) Inserts empty rows.booleanisCellEditable(int row, int column) Returns true if the cell at the given location is an editable cellbooleanWhether the first row of the sheet holds the names of the columns.voidremoveColumns(int column, int count) Removes columns.voidRemoves a listener to the data changed eventvoidremoveRows(int row, int count) Removes rows.voidsetCellFormat(int row, int column, String format) Sets the number format of a cell: how its value is shown, and for a number whether it is a date.voidsetCellFormula(int row, int column, String formula) Puts a formula into a cell and recalculates what depends on it.voidsetCellFormula(String address, String formula) Puts a formula into a cell named the way a formula names it.voidsetCellValue(int row, int column, Object value) Stores a value in a cell, replacing a formula it held, and recalculates what depends on it.voidsetCellValue(String address, Object value) Stores a value in a cell named the way a formula names it.voidsetColumnWidth(int column, double characters) Sets the width of a column.voidsetEditable(boolean editable) Sets whether the table model reports its cells as editable.voidsetFrozenPanes(int rows, int columns) Sets the rows and columns that stay in place while the rest of the sheet scrolls.voidsetHeaderRow(boolean headerRow) Sets whether the first row of the sheet holds the names of the columns.voidsetRowOutlineLevel(int row, int level) Sets the outline level of a row.voidsetValueAt(int row, int column, Object o) Stores a value.Methods inherited from class AbstractTableModel
getCellType, getMultipleChoiceOptions, getValidationConstraint, getValidator, setValidator
-
Method Details
-
getName
The name of the sheet, which formulas on other sheets refer to it by.
Returns
the name
-
getWorkbook
The workbook the sheet is part of.
Returns
the workbook
-
getCellValue
-
getCellValue
The value of a cell named the way a formula names it.
Parameters
address: an address such asB7
Returns
the value, see
getCellValue(int, int) -
setCellValue
Stores a value in a cell, replacing a formula it held, and recalculates what depends on it.
Parameters
-
row: the row of the sheet -
column: the column of the sheet -
value: aNumber,String,Boolean,DateorCellError; null empties the cell. AStringis stored as text even when it looks like a number or a formula.
-
-
setCellValue
Stores a value in a cell named the way a formula names it.
Parameters
-
address: an address such asB7 -
value: the value, seesetCellValue(int, int, Object)
-
-
getCellFormula
The formula of a cell.
Parameters
-
row: the row of the sheet -
column: the column of the sheet
Returns
the formula with its leading
=, or null when the cell holds no formula -
-
setCellFormula
Puts a formula into a cell and recalculates what depends on it.
Parameters
-
row: the row of the sheet -
column: the column of the sheet -
formula: a formula in Excel's syntax, with or without the leading=
Throws
IllegalArgumentException: when the formula cannot be read
-
-
setCellFormula
Puts a formula into a cell named the way a formula names it.
Parameters
-
address: an address such asB7 -
formula: the formula, seesetCellFormula(int, int, String)
-
-
getCellText
The text a cell shows: its value in its number format.
Parameters
-
row: the row of the sheet -
column: the column of the sheet
Returns
the text, empty for an empty cell
-
-
getCellFormat
The number format of a cell.
Parameters
-
row: the row of the sheet -
column: the column of the sheet
Returns
a format code such as
#,##0.00,Generalwhen the cell has none -
-
setCellFormat
Sets the number format of a cell: how its value is shown, and for a number whether it is a date.
Parameters
-
row: the row of the sheet -
column: the column of the sheet -
format: a format code in Excel's syntax, such as0.0%ord mmm yyyy; null for none
Throws
IllegalArgumentException: for a format code no file can hold: more than four sections, or a quote, bracket or escape left open
-
-
isHeaderRow
public boolean isHeaderRow()Whether the first row of the sheet holds the names of the columns. When it does, the table model's rows start under it.
Returns
true when the first row is a header
-
setHeaderRow
public void setHeaderRow(boolean headerRow) Sets whether the first row of the sheet holds the names of the columns.
Parameters
headerRow: true to take the column names from the first row
-
setEditable
public void setEditable(boolean editable) Sets whether the table model reports its cells as editable.
Parameters
editable: false for a grid that only shows the sheet
-
getRowCount
public int getRowCount()Description copied from interface:TableModelReturns the number of rows in the table
Returns
the number of rows in the table
-
getColumnCount
public int getColumnCount()Description copied from interface:TableModelReturns the number of columns in the table
Returns
the number of columns in the table
-
getColumnName
The name of a column: the text of the header row's cell when the sheet has a header row and the cell is not empty, the letters of the column otherwise. -
isCellEditable
public boolean isCellEditable(int row, int column) Description copied from interface:TableModelReturns true if the cell at the given location is an editable cell
Parameters
-
row: the cell row -
column: the cell column
Returns
true if the cell at the given location is an editable cell
-
-
getValueAt
Description copied from interface:TableModelReturns the value of the cell at the given location
Parameters
-
row: the cell row -
column: the cell column
Returns
the value of the cell at the given location
-
-
setValueAt
Stores a value. AStringis read as if it had been typed into a spreadsheet, see the class description; anything else is stored as it is. -
getFormulaAt
The formula of a cell of the table model.
Parameters
-
row: the row of the model -
column: the column
Returns
the formula with its leading
=, or null -
-
addDataChangeListener
Adds a listener that is told when a cell changes: the cell that was edited, and every formula that reads it, directly or through other formulas.
A formula is calculated when its value is asked for, not when what it reads changes, so the event says that the cell is out of date. A cell that is out of date already, because nobody read it since the last event, is not announced a second time.
Parameters
d: the listener
-
removeDataChangeListener
Description copied from interface:TableModelRemoves a listener to the data changed event
Parameters
d: the listener to remove
-
addRow
Adds a row under the last one.
Parameters
values: the values of the row's cells from the first column on; each is stored assetCellValue(int, int, Object)stores it
-
insertRows
public void insertRows(int row, int count) Inserts empty rows. Formulas that refer to cells under them are rewritten to follow the cells, as a spreadsheet does.
Parameters
-
row: the row of the sheet the first new row becomes -
count: how many rows to insert
Throws
IllegalArgumentException: for a row outside the sheet or a negative count
-
-
removeRows
public void removeRows(int row, int count) Removes rows. A formula that referred to a removed cell evaluates to
#REF!; a range that included removed rows shrinks.Parameters
-
row: the first row of the sheet to remove -
count: how many rows to remove
Throws
IllegalArgumentException: for a row outside the sheet or a negative count
-
-
insertColumns
public void insertColumns(int column, int count) Inserts empty columns. Formulas follow the cells that move.
Parameters
-
column: the column the first new column becomes -
count: how many columns to insert
Throws
IllegalArgumentException: for a column outside the sheet or a negative count
-
-
removeColumns
public void removeColumns(int column, int count) Removes columns. A formula that referred to a removed cell evaluates to
#REF!.Parameters
-
column: the first column to remove -
count: how many columns to remove
Throws
IllegalArgumentException: for a column outside the sheet or a negative count
-
-
createColumns
Describes the columns of the sheet for a
DataGrid: the kind of each -- number, date, logical, text -- from the values it holds, a formatter that shows values in the number format most of the column's cells have, and the width the file gave the column.The columns describe the sheet as it is now. Call this again after loading other data into it.
Returns
one column per column of the sheet
-
getColumnWidth
public double getColumnWidth(int column) The width of a column.
Parameters
column: the column
Returns
the width in characters of the default font, the unit spreadsheet files use; 0 when the column has the default width
-
setColumnWidth
public void setColumnWidth(int column, double characters) Sets the width of a column.
Parameters
-
column: the column -
characters: the width in characters of the default font; 0 for the default width
Throws
IllegalArgumentException: for a width that is negative, not a number or infinite, which no file can hold, or a column outside the sheet
-
-
getRowOutlineLevel
public int getRowOutlineLevel(int row) The outline level of a row: how deep it is nested in the groups a spreadsheet can collapse.
Parameters
row: the row of the sheet
Returns
the level, 0 for a row in no group
-
setRowOutlineLevel
public void setRowOutlineLevel(int row, int level) Sets the outline level of a row.
Parameters
-
row: the row of the sheet -
level: the level, 0 to 7
Throws
IllegalArgumentException: for a row outside the sheet, or a level outside 0 to 7, the levels the file formats hold
-
-
getFrozenRows
public int getFrozenRows()The number of rows at the top that stay in place while the rest scrolls.
Returns
the count of frozen rows
-
getFrozenColumns
public int getFrozenColumns()The number of columns at the left that stay in place while the rest scrolls.
Returns
the count of frozen columns
-
setFrozenPanes
public void setFrozenPanes(int rows, int columns) Sets the rows and columns that stay in place while the rest of the sheet scrolls.
Parameters
-
rows: the count of rows at the top -
columns: the count of columns at the left
Throws
IllegalArgumentException: for a negative count, or one that leaves no row or column of the sheet to scroll
-
-
addMergedRegion
public void addMergedRegion(int firstRow, int firstColumn, int lastRow, int lastColumn) Merges a rectangle of cells into one, as the file formats record it. The value shown is the one of the top left cell.
Parameters
-
firstRow: the top row -
firstColumn: the left column -
lastRow: the bottom row -
lastColumn: the right column
Throws
IllegalArgumentException: for a rectangle outside the sheet or over a merge the sheet has
-
-
getMergedRegionCount
public int getMergedRegionCount()The number of merged regions.
Returns
the count
-
getMergedRegion
public int[] getMergedRegion(int index) A merged region.
Parameters
index: which one, from 0
Returns
a new array of the top row, left column, bottom row and right column
-