set JTable Columns Percentage - Java Swing

Java examples for Swing:JTable Column

Description

set JTable Columns Percentage

Demo Code


//package com.java2s;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

public class Main {
    public static void setJTableColumnsPercentage(JTable table,
            int tablePreferredWidth, double... percentages) {
        double total = 0;
        for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
            total += percentages[i];//from   w  w  w.j ava2  s . c om
        }

        for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth((int) (tablePreferredWidth * (percentages[i] / total)));
        }
    }
}

Related Tutorials