Java JTable Row packRows(JTable table, int start, int end, int margin)

Here you can find the source of packRows(JTable table, int start, int end, int margin)

Description

pack Rows

License

Apache License

Declaration

private static void packRows(JTable table, int start, int end, int margin) 

Method Source Code


//package com.java2s;
/*/*from  w w  w .j a  v a  2  s.c  o  m*/
   Copyright 2011 Udayakumar Dhansingh (Udy)
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
       
 */

import java.awt.Component;
import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;

public class Main {
    public static void packRows(JTable table, int margin) {
        packRows(table, 0, table.getRowCount(), margin);
    }

    private static void packRows(JTable table, int start, int end, int margin) {
        for (int r = 0; r < table.getRowCount(); r++) {
            // Get the preferred height
            int h = getPreferredRowHeight(table, r, margin);

            // Now set the row height using the preferred height
            if (table.getRowHeight(r) != h) {
                table.setRowHeight(r, h);
            }
        }
    }

    private static int getPreferredRowHeight(JTable table, int rowIndex, int margin) {
        // Get the current default height for all rows
        int height = table.getRowHeight();

        // Determine highest cell in the row
        for (int c = 0; c < table.getColumnCount(); c++) {
            TableCellRenderer renderer = table.getCellRenderer(rowIndex, c);
            Component comp = table.prepareRenderer(renderer, rowIndex, c);
            int h = comp.getPreferredSize().height + 2 * margin;
            height = Math.max(height, h);
        }
        return height;
    }
}

Related

  1. insertRow(final JTable table, final int index, Object... data)
  2. isFirstToLastRow(TableModelEvent e)
  3. isRowInsert(TableModelEvent e)
  4. jtable$convertRowIndexToModel(JTable table, int index)
  5. makeIntoMoveRowUpButton(final JTable table, JButton button)
  6. removeTableRows(DefaultTableModel model)
  7. rowToModelIndex(JTable table, int row)
  8. setRowLines(JTable table, int row, int lines)
  9. setTableSize(JTable table, float percentage, int rows)