Class ViewHugeTableModel
- All Implemented Interfaces:
TableModel
The point of this is for displaying tables with very large row counts
in a JTable.
Since the default height of a JTable row is 16 pixels
(see JTable.getRowHeight()
) a JTable component
with more than 2^27 rows (about 134 million) will have a height
in pixels which is too large to represent in a 32-bit int.
In that case Swing gives up and just doesn't display the thing.
This class implements a model of a fixed (large but not too large)
size, VIEWSIZE
rows, which at any one time gives a view of
a contiguous sequence of rows from the base table, starting at a
mutable value given by getViewBase()
. It works out the value
of viewBase by reference to a scrollbar which is owned by
this model, and assumed to control vertical scrolling of the
scroll panel displaying the JTable that views it.
The viewBase value is updated dynamically according to the current
state (position) of the scrollbar. Whenever the viewBase changes,
listeners (most importantly the client JTable) are notified.
In principle, every time the scrollbar position, or equivalently the set of currently visible rows, changes, the viewBase should change. But since the scrollbar has only pixel resolution, smallish changes can be accommodated without needing to take the somewhat expensive step of changing the viewBase.
The underlying table model can still only have up to 2^31 rows. The general approach used here would work for larger underlying row data, but it would be necessary to define a new long-capable TableModel interface to accommodate such data.
It is not recommended to use this class for underlying table models which are not in fact huge.
Note this class will only help you for displaying a JTable within a scrollpane. If you want to display a giga-row JTable unscrolled on a 100km tall VDU, you're on your own.
- Since:
- 16 Jul 2014
- Author:
- Mark Taylor
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Name of property giving underlying value of first row of this model.static final int
Fixed number of rows displayed by this model. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty model.ViewHugeTableModel
(TableModel hugeModel, JScrollBar vbar) Constructs a configured model. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a listener that will be notified about ViewBase changes.void
void
configureModel
(TableModel hugeModel, JScrollBar vbar) Sets this mode up for use with a given underlying table model and scroll bar.protected void
Notifies property change listeners of a property change.protected void
Notifies table model listeners of a table model event.Class
<?> getColumnClass
(int icol) int
getColumnName
(int icol) int
getHugeRow
(int iViewRow) Returns the row in the underlying huge model corresponding to a given row in this view.int
getValueAt
(int irow, int icol) int
Returns the offset from which this view views the underlying table model.boolean
isCellEditable
(int irow, int icol) void
Removes a listener added earlier.void
void
setValueAt
(Object value, int irow, int icol)
-
Field Details
-
VIEWBASE_PROPERTY
Name of property giving underlying value of first row of this model.- See Also:
-
VIEWSIZE
public static final int VIEWSIZEFixed number of rows displayed by this model.- See Also:
-
-
Constructor Details
-
ViewHugeTableModel
public ViewHugeTableModel()Constructs an empty model. -
ViewHugeTableModel
Constructs a configured model.- Parameters:
hugeModel
- table model with more than VIEWSIZE rowsvbar
- scrollbar used to control vertical scrolling of table view
-
-
Method Details
-
configureModel
Sets this mode up for use with a given underlying table model and scroll bar.- Parameters:
hugeModel
- table model with more than VIEWSIZE rowsvbar
- scrollbar used to control vertical scrolling of table view
-
getRowCount
public int getRowCount()- Specified by:
getRowCount
in interfaceTableModel
-
getValueAt
- Specified by:
getValueAt
in interfaceTableModel
-
setValueAt
- Specified by:
setValueAt
in interfaceTableModel
-
isCellEditable
public boolean isCellEditable(int irow, int icol) - Specified by:
isCellEditable
in interfaceTableModel
-
getColumnCount
public int getColumnCount()- Specified by:
getColumnCount
in interfaceTableModel
-
getColumnName
- Specified by:
getColumnName
in interfaceTableModel
-
getColumnClass
- Specified by:
getColumnClass
in interfaceTableModel
-
addTableModelListener
- Specified by:
addTableModelListener
in interfaceTableModel
-
removeTableModelListener
- Specified by:
removeTableModelListener
in interfaceTableModel
-
getViewBase
public int getViewBase()Returns the offset from which this view views the underlying table model. Changes in this value will be notified to registered PropertyChangeListeners.- Returns:
- view base
-
addPropertyChangeListener
Adds a listener that will be notified about ViewBase changes. These have the property nameVIEWBASE_PROPERTY
.- Parameters:
lnr
- listener to add
-
removePropertyChangeListener
Removes a listener added earlier.- Parameters:
lnr
- listener to remove
-
fireTableChanged
Notifies table model listeners of a table model event.- Parameters:
evt
- event
-
firePropertyChanged
Notifies property change listeners of a property change.- Parameters:
evt
- event
-
getHugeRow
public int getHugeRow(int iViewRow) Returns the row in the underlying huge model corresponding to a given row in this view.- Returns:
iViewRow + viewBase
-