JTable自动调整,扭曲?

时间:2010-06-24 15:38:39

标签: jtable

这是一个众所周知的JTable autoresizer:

public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);

        int margin = 5;

        for (int i = 0; i < table.getColumnCount(); i++) {
            int                     vColIndex = i;
            DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn             col       = colModel.getColumn(vColIndex);
            int                     width     = 0;

            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();

            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

            width = comp.getPreferredSize().width;

            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }

            // Add margin
            width += 2 * margin;

            // Set the width
            col.setPreferredWidth(width);
        }

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);

        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);

        return table;
    }

这里的问题是返回的表收缩(宽度)

因此,如果我的JTable组件是100%,则返回的列区域可能只有70%(比方说)

如果我想要自动调整大小,请记住整个JTable的原始设置宽度。因此,即使在调整大小后,所有列都能正确匹配。

1 个答案:

答案 0 :(得分:1)

我设法改进了上面的代码,以便考虑表格大小:

public static void resizeTable(JTable table) {
    int margin = 5;
    int tableWidth = 0;
    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        TableColumn col = table.getColumnModel().getColumn(vColIndex);
        int width = 0;
        // Get width of column header
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
        width = comp.getPreferredSize().width;
        // Get maximum width of column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                    r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }
        // Add margin
        width += 2 * margin;
        // Set the width
        col.setPreferredWidth(width);
        tableWidth += width;
    }

    if (tableWidth < table.getParent().getWidth()) {
        tableWidth = table.getParent().getWidth();
        for (int i = 0; i < table.getColumnCount(); i++) {
            int vColIndex = i;
            TableColumn col = table.getColumnModel().getColumn(vColIndex);
            int width = 0;
            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();
            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }
            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
            width = comp.getPreferredSize().width;
            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }
            // Add margin
            width += 2 * margin;

            // Set the width based on table width
            if (width < tableWidth / (table.getColumnCount() - i)) {
                width = tableWidth / (table.getColumnCount() - i);
            }
            tableWidth -= width;
            col.setPreferredWidth(width);
        }
    }
}

请注意,表数据中的循环执行两次。此外,我更喜欢该函数独立于JTable实例,以便能够从我项目中的任何JTable调用它。