Java JTable Column Width Set setColumnSizes(JTable table, double[] percentages)

Here you can find the source of setColumnSizes(JTable table, double[] percentages)

Description

set Column Sizes

License

Open Source License

Declaration

public static void setColumnSizes(JTable table, double[] percentages) 

Method Source Code


//package com.java2s;
/*/* www.j  a  va2 s  .  co  m*/
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details:
 * http://www.gnu.org/licenses/gpl.txt
 */

import java.awt.Dimension;

import javax.swing.JTable;
import javax.swing.table.TableColumn;

public class Main {
    public static void setColumnSizes(JTable table, double[] percentages) {
        Dimension tableDim = table.getPreferredSize();
        tableDim = table.getSize();

        double total = 0;
        for (double t : percentages)
            total += t;

        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth((int) (tableDim.width * (percentages[i] / total)));
            //column.setMaxWidth((int) (tableDim.width * (percentages[i] / total)));
            //column.setMinWidth((int) (tableDim.width * (percentages[i] / total)));
        }
    }
}

Related

  1. initTableWidth(JTable table, int[] colWiths)
  2. makeTableColumnWidthFit(JTable jTable, int col, int margin)
  3. setColumnMaxWidths(JTable tbl, Integer... widths)
  4. setColumnMinWidths(final JTable table)
  5. setColumnSize(TableColumnModel cm, int id, int percent, int tablesize, boolean resizeable)
  6. setColumnsSize(TableColumnModel cm, int tablesize)
  7. setColumnWidth(JTable table, int columnIdx, int width)
  8. setColumnWidth(JTable table, int columnIndex, int preferredWidth)
  9. setColumnWidth(JTable table, int columnNumber, int width)