Class JTableHelper


  • public class JTableHelper
    extends Object
    A helper class for JTable, e.g. calculating the optimal colwidth.
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    See Also:
    JTableHelper
    • Field Detail

      • LOGGER

        protected static Logger LOGGER
        for logging.
      • MAX_ROWS

        public static final int MAX_ROWS
        the maximum number of rows to use for calculation.
        See Also:
        Constant Field Values
      • MAX_CHARS

        public static final int MAX_CHARS
        the maximum number of characters before skipping optimal column width.
        See Also:
        Constant Field Values
      • m_Table

        protected JTable m_Table
        the table to work with.
    • Constructor Detail

      • JTableHelper

        public JTableHelper​(JTable table)
        initializes the object.
        Parameters:
        table - the table to work on
    • Method Detail

      • getJTable

        public JTable getJTable()
        returns the JTable.
        Returns:
        the table to work on
      • calcColumnWidth

        public int calcColumnWidth​(int col)
        calcs the optimal column width of the given column.
        Parameters:
        col - the column index
        Returns:
        the optimal width
      • calcColumnWidthBounded

        public int calcColumnWidthBounded​(int col,
                                          int max)
        calcs the optimal column width of the given column.
        Parameters:
        col - the column index
        max - the limit for the column width, -1 for unlimited
        Returns:
        the optimal width
      • calcColumnWidth

        public static int calcColumnWidth​(JTable table,
                                          int col)
        Calculates the optimal width for the column of the given table. The calculation is based on the preferred width of the header and cell renderer.
        Taken from the newsgroup de.comp.lang.java with some modifications.
        Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/
        Parameters:
        table - the table to calculate the column width
        col - the column to calculate the widths
        Returns:
        the width, -1 if error
      • calcColumnWidthBounded

        public static int calcColumnWidthBounded​(JTable table,
                                                 int col,
                                                 int max)
        Calculates the optimal width for the column of the given table. The calculation is based on the preferred width of the header and cell renderer.
        Taken from the newsgroup de.comp.lang.java with some modifications.
        Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/
        Parameters:
        table - the table to calculate the column width
        col - the column to calculate the widths
        max - the limit for the column width, -1 for unlimited
        Returns:
        the width, -1 if error
      • calcHeaderWidth

        public int calcHeaderWidth​(int col)
        calcs the optimal header width of the given column.
        Parameters:
        col - the column index
        Returns:
        the optimal width
      • calcHeaderWidthBounded

        public int calcHeaderWidthBounded​(int col,
                                          int max)
        calcs the optimal header width of the given column.
        Parameters:
        col - the column index
        max - the limit for the column width, -1 for unlimited
        Returns:
        the optimal width
      • calcHeaderWidth

        public static int calcHeaderWidth​(JTable table,
                                          int col)
        Calculates the optimal width for the header of the given table. The calculation is based on the preferred width of the header renderer.
        Parameters:
        table - the table to calculate the column width
        col - the column to calculate the widths
        Returns:
        the width, -1 if error
      • calcHeaderWidthBounded

        public static int calcHeaderWidthBounded​(JTable table,
                                                 int col,
                                                 int max)
        Calculates the optimal width for the header of the given table. The calculation is based on the preferred width of the header renderer.
        Parameters:
        table - the table to calculate the column width
        col - the column to calculate the widths
        max - the limit for the column width, -1 for unlimited
        Returns:
        the width, -1 if error
      • setOptimalColumnWidth

        public void setOptimalColumnWidth​(int col)
        sets the optimal column width for the given column.
        Parameters:
        col - the column index
      • setOptimalColumnWidthBounded

        public void setOptimalColumnWidthBounded​(int col,
                                                 int max)
        sets the optimal column width for the given column.
        Parameters:
        col - the column index
        max - the maximum column width, -1 for unlimited
      • setOptimalColumnWidth

        public static void setOptimalColumnWidth​(JTable table,
                                                 int col)
        sets the optimal column width for the given column.
        Parameters:
        table - the table to work on
        col - the column index
      • setOptimalColumnWidthBounded

        public static void setOptimalColumnWidthBounded​(JTable table,
                                                        int col,
                                                        int max)
        sets the optimal column width for the given column.
        Parameters:
        table - the table to work on
        col - the column index
        max - the maximum column width, -1 for unlimited
      • setOptimalColumnWidths

        public void setOptimalColumnWidths​(int[] cols)
        sets the optimal column width for the given columns.
        Parameters:
        cols - the column indices
      • setOptimalColumnWidths

        public static void setOptimalColumnWidths​(JTable table,
                                                  int[] cols)
        sets the optimal column width for the given columns.
        Parameters:
        table - the table to work on
        cols - the column indices
      • setOptimalColumnWidthsBounded

        public void setOptimalColumnWidthsBounded​(int[] cols,
                                                  int max)
        sets the optimal column width for the given columns.
        Parameters:
        cols - the column indices
        max - the maximum column width, -1 for unlimited
      • setOptimalColumnWidthsBounded

        public static void setOptimalColumnWidthsBounded​(JTable table,
                                                         int[] cols,
                                                         int max)
        sets the optimal column width for the given columns.
        Parameters:
        table - the table to work on
        cols - the column indices
        max - the maximum column width, -1 for unlimited
      • setOptimalColumnWidth

        public void setOptimalColumnWidth()
        sets the optimal column width for all columns.
      • setOptimalColumnWidthBounded

        public void setOptimalColumnWidthBounded​(int max)
        sets the optimal column width for all columns.
      • setOptimalColumnWidth

        public static void setOptimalColumnWidth​(JTable table)
        sets the optimal column width for alls column if the given table.
        Parameters:
        table - the table to work on
      • setOptimalColumnWidthBounded

        public static void setOptimalColumnWidthBounded​(JTable table,
                                                        int max)
        sets the optimal column width for alls column if the given table.
        Parameters:
        table - the table to work on
      • setOptimalHeaderWidth

        public void setOptimalHeaderWidth​(int col)
        sets the optimal header width for the given column.
        Parameters:
        col - the column index
      • setOptimalHeaderWidthBounded

        public void setOptimalHeaderWidthBounded​(int col,
                                                 int max)
        sets the optimal header width for the given column.
        Parameters:
        col - the column index
        max - the column width limit, -1 for unlimited
      • setOptimalHeaderWidth

        public static void setOptimalHeaderWidth​(JTable table,
                                                 int col)
        sets the optimal header width for the given column.
        Parameters:
        table - the table to work on
        col - the column index
      • setOptimalHeaderWidthBounded

        public static void setOptimalHeaderWidthBounded​(JTable table,
                                                        int col,
                                                        int max)
        sets the optimal header width for the given column.
        Parameters:
        table - the table to work on
        col - the column index
        max - the column width limit, -1 for unlimited
      • setOptimalHeaderWidth

        public void setOptimalHeaderWidth()
        sets the optimal header width for all columns.
      • setOptimalHeaderWidth

        public static void setOptimalHeaderWidth​(JTable table)
        sets the optimal header width for alls column if the given table.
        Parameters:
        table - the table to work with
      • scrollToVisible

        public void scrollToVisible​(int row,
                                    int col)
        Assumes table is contained in a JScrollPane. Scrolls the cell (rowIndex, vColIndex) so that it is visible within the viewport.
        Parameters:
        row - the row index
        col - the column index
      • scrollToVisible

        public static void scrollToVisible​(JTable table,
                                           int row,
                                           int col)
        Assumes table is contained in a JScrollPane. Scrolls the cell (rowIndex, vColIndex) so that it is visible within the viewport.
        Parameters:
        table - the table to work with
        row - the row index
        col - the column index
      • toSpreadSheet

        public static SpreadSheet toSpreadSheet​(JTable table)
        Turns a table into a spreadsheet.
        Parameters:
        table - the table to convert
        Returns:
        the generated spreadsheet
      • toSpreadSheet

        public static SpreadSheet toSpreadSheet​(TableModel model)
        Turns a table model into a spreadsheet.
        Parameters:
        model - the table model to convert
        Returns:
        the generated spreadsheet