Java JTable Column Size Calculate fixSize(TableColumn column, JTable table)

Here you can find the source of fixSize(TableColumn column, JTable table)

Description

Fix the size of a column according to the header text.

License

Apache License

Parameter

Parameter Description
column to be resized
table containing the column

Declaration

public static void fixSize(TableColumn column, JTable table) 

Method Source Code

//package com.java2s;
/*// www.j  a  va 2 s  . c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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;
import javax.swing.table.TableColumn;

public class Main {
    /**
     * Fix the size of a column according to the header text.
     * 
     * @param column to be resized
     * @param table containing the column
     */
    public static void fixSize(TableColumn column, JTable table) {
        TableCellRenderer rndr;
        rndr = column.getHeaderRenderer();
        if (rndr == null) {
            rndr = table.getTableHeader().getDefaultRenderer();
        }
        Component c = rndr.getTableCellRendererComponent(table,
                column.getHeaderValue(), false, false, -1,
                column.getModelIndex());
        int width = c.getPreferredSize().width + 10;
        column.setMaxWidth(width);
        column.setPreferredWidth(width);
        column.setResizable(false);
    }
}

Related

  1. calcColumnWidths(JTable table)
  2. calculateColumnWidth(JTable table, int columnIndex)
  3. calculateColumnWidth(JTable table, int columnIndex)
  4. calculateColumnWidth_1(JTable table, int pos)
  5. fixedColumSize(TableColumn c, int width)
  6. initColumnSizes(JTable table)
  7. initColumnSizes(JTable table, boolean[] show)
  8. initColumnSizes(JTable table, int rowStart, int rowEnd)
  9. initializeTableColumns(JTable table, int size[])