Class TableRowHeader

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CellEditorListener, ListSelectionListener, RowSorterListener, TableColumnModelListener, TableModelListener, Scrollable

public class TableRowHeader extends JTable
Provides a component suitable for use as a rowHeader component in the same JScrollPane as is being used to house a JTable. It displays the row indices starting at 1 and increasing. If you want some other number to be displayed, override the rowNumber(int) method.

You would normally use this class as follows:

     JTable jtab = ...
     JScrollPane scrollpane = new JScrollPane( jtab );
     scrollpane.setRowHeaderView( new TableRowHeader( jtab ) );
 
This header will register itself as a listener on the master table's model so that it can respond to changes. In the event that the master JTable's model changes during the lifetime of this header table, then modelChanged() should be called. At construction time the master table's selection model will be installed in this header too, so that you can make row selections by clicking on the header or the master.
Author:
Mark Taylor (Starlink)
See Also:
  • Constructor Details

    • TableRowHeader

      public TableRowHeader(JTable table)
      Construct a new TableRowHeader.
  • Method Details

    • setLongestNumber

      public void setLongestNumber(long num)
      Sets the longest value that will be used in the row header field. This is used to configure the size of the column in pixels, so the value used should be the one that generates the longest text; typically the largest number, but for instance "-1" is longer than "1".

      If no value is set, a guess will be made based on the number of rows.

      Parameters:
      num - longest number
    • getPreferredScrollableViewportSize

      public Dimension getPreferredScrollableViewportSize()
      Specified by:
      getPreferredScrollableViewportSize in interface Scrollable
      Overrides:
      getPreferredScrollableViewportSize in class JTable
    • modelChanged

      public void modelChanged()
      This method should be called to notify this header that the master table's TableModel has been changed.
    • rowNumber

      public long rowNumber(int irow)
      Determines the numeric index to be displayed for a given row number into the table. The default implementation returns irow+1 so that the first row is labelled 1, the second one 2 etc, but this method may be overridden for more specialised behaviour.
      Parameters:
      irow - the row index of the displayed row (starts at zero)
      Returns:
      the number of the row it should be labelled
    • installOnScroller

      public void installOnScroller(JScrollPane scroller)
      Installs this row header on a scroll pane.
      Parameters:
      scroller - scroll pane for which this component will be the row header