Class ViewHugeTableModel

  • All Implemented Interfaces:
    javax.swing.table.TableModel

    public class ViewHugeTableModel
    extends java.lang.Object
    implements javax.swing.table.TableModel
    Swing TableModel implementation which provides a view on a very large table model.

    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

      Fields 
      Modifier and Type Field Description
      static java.lang.String VIEWBASE_PROPERTY
      Name of property giving underlying value of first row of this model.
      static int VIEWSIZE
      Fixed number of rows displayed by this model.
    • Constructor Summary

      Constructors 
      Constructor Description
      ViewHugeTableModel()
      Constructs an empty model.
      ViewHugeTableModel​(javax.swing.table.TableModel hugeModel, javax.swing.JScrollBar vbar)
      Constructs a configured model.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPropertyChangeListener​(java.beans.PropertyChangeListener lnr)
      Adds a listener that will be notified about ViewBase changes.
      void addTableModelListener​(javax.swing.event.TableModelListener lnr)  
      void configureModel​(javax.swing.table.TableModel hugeModel, javax.swing.JScrollBar vbar)
      Sets this mode up for use with a given underlying table model and scroll bar.
      protected void firePropertyChanged​(java.beans.PropertyChangeEvent evt)
      Notifies property change listeners of a property change.
      protected void fireTableChanged​(javax.swing.event.TableModelEvent evt)
      Notifies table model listeners of a table model event.
      java.lang.Class<?> getColumnClass​(int icol)  
      int getColumnCount()  
      java.lang.String getColumnName​(int icol)  
      int getHugeRow​(int iViewRow)
      Returns the row in the underlying huge model corresponding to a given row in this view.
      int getRowCount()  
      java.lang.Object getValueAt​(int irow, int icol)  
      int getViewBase()
      Returns the offset from which this view views the underlying table model.
      boolean isCellEditable​(int irow, int icol)  
      void removePropertyChangeListener​(java.beans.PropertyChangeListener lnr)
      Removes a listener added earlier.
      void removeTableModelListener​(javax.swing.event.TableModelListener lnr)  
      void setValueAt​(java.lang.Object value, int irow, int icol)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • VIEWBASE_PROPERTY

        public static final java.lang.String VIEWBASE_PROPERTY
        Name of property giving underlying value of first row of this model.
        See Also:
        Constant Field Values
      • VIEWSIZE

        public static final int VIEWSIZE
        Fixed number of rows displayed by this model.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ViewHugeTableModel

        public ViewHugeTableModel()
        Constructs an empty model.
      • ViewHugeTableModel

        public ViewHugeTableModel​(javax.swing.table.TableModel hugeModel,
                                  javax.swing.JScrollBar vbar)
        Constructs a configured model.
        Parameters:
        hugeModel - table model with more than VIEWSIZE rows
        vbar - scrollbar used to control vertical scrolling of table view
    • Method Detail

      • configureModel

        public void configureModel​(javax.swing.table.TableModel hugeModel,
                                   javax.swing.JScrollBar vbar)
        Sets this mode up for use with a given underlying table model and scroll bar.
        Parameters:
        hugeModel - table model with more than VIEWSIZE rows
        vbar - scrollbar used to control vertical scrolling of table view
      • getRowCount

        public int getRowCount()
        Specified by:
        getRowCount in interface javax.swing.table.TableModel
      • getValueAt

        public java.lang.Object getValueAt​(int irow,
                                           int icol)
        Specified by:
        getValueAt in interface javax.swing.table.TableModel
      • setValueAt

        public void setValueAt​(java.lang.Object value,
                               int irow,
                               int icol)
        Specified by:
        setValueAt in interface javax.swing.table.TableModel
      • isCellEditable

        public boolean isCellEditable​(int irow,
                                      int icol)
        Specified by:
        isCellEditable in interface javax.swing.table.TableModel
      • getColumnCount

        public int getColumnCount()
        Specified by:
        getColumnCount in interface javax.swing.table.TableModel
      • getColumnName

        public java.lang.String getColumnName​(int icol)
        Specified by:
        getColumnName in interface javax.swing.table.TableModel
      • getColumnClass

        public java.lang.Class<?> getColumnClass​(int icol)
        Specified by:
        getColumnClass in interface javax.swing.table.TableModel
      • addTableModelListener

        public void addTableModelListener​(javax.swing.event.TableModelListener lnr)
        Specified by:
        addTableModelListener in interface javax.swing.table.TableModel
      • removeTableModelListener

        public void removeTableModelListener​(javax.swing.event.TableModelListener lnr)
        Specified by:
        removeTableModelListener in interface javax.swing.table.TableModel
      • 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

        public void addPropertyChangeListener​(java.beans.PropertyChangeListener lnr)
        Adds a listener that will be notified about ViewBase changes. These have the property name VIEWBASE_PROPERTY.
        Parameters:
        lnr - listener to add
      • removePropertyChangeListener

        public void removePropertyChangeListener​(java.beans.PropertyChangeListener lnr)
        Removes a listener added earlier.
        Parameters:
        lnr - listener to remove
      • fireTableChanged

        protected void fireTableChanged​(javax.swing.event.TableModelEvent evt)
        Notifies table model listeners of a table model event.
        Parameters:
        evt - event
      • firePropertyChanged

        protected void firePropertyChanged​(java.beans.PropertyChangeEvent evt)
        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