简体   繁体   English

自动调整JTable列的宽度

[英]Auto resize the widths of JTable's columns dynamically

I have a JTable with 3 columns: 我有一个有3列的JTable:

- No. #
- Name
- PhoneNumber

I want to make specific width for each column as follows: 我想为每列制作特定的宽度,如下所示:

在此输入图像描述

and I want the JTable able to update the widths of its columns dynamically if needed (for example, inserting large number in the column # ) and keeping same style of the JTable 我希望JTable能够在需要时动态更新其列的宽度(例如,在#列中插入大数字)并保持JTable的相同样式

I solved the first issue, using this code: 我使用以下代码解决了第一个问题:

myTable.getColumnModel().getColumn(columnNumber).setPreferredWidth(columnWidth);

but I didn't success to make myTable to update the widths dynamically ONLY if the current width of the column doesn't fit its contents. 但是,如果列的当前宽度不适合其内容,我没有成功使myTable动态更新宽度。 Can you help me solving this issue? 你能帮我解决这个问题吗?

Here I found my answer: http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/ 在这里,我找到了答案: http//tips4java.wordpress.com/2008/11/10/table-column-adjuster/
The idea is to check some rows' content length to adjust the column width. 我们的想法是检查一些行的内容长度以调整列宽。
In the article, the author provided a full code in a downloadable java file. 在文章中,作者在可下载的java文件中提供了完整的代码。

JTable table = new JTable( ... );
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

for (int column = 0; column < table.getColumnCount(); column++)
{
    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    int preferredWidth = tableColumn.getMinWidth();
    int maxWidth = tableColumn.getMaxWidth();

    for (int row = 0; row < table.getRowCount(); row++)
    {
        TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
        Component c = table.prepareRenderer(cellRenderer, row, column);
        int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
        preferredWidth = Math.max(preferredWidth, width);

        //  We've exceeded the maximum width, no need to check other rows

        if (preferredWidth >= maxWidth)
        {
            preferredWidth = maxWidth;
            break;
        }
    }

    tableColumn.setPreferredWidth( preferredWidth );
}

Use the addRow(...) method of the DefaultTableModel to add data to the table dynamically. 使用DefaultTableModel的addRow(...)方法动态地向表中添加数据。

Update: 更新:

To adjust the width of a visible column I think you need to use: 要调整可见列的宽度,我认为您需要使用:

tableColumn.setWidth(...);

I actually run into this problem too. 我实际上也遇到了这个问题。 I've found one useful link that solved my issue. 我找到了一个有用的链接来解决我的问题。 Pretty much get the specific column and set its setMinWidth and setMaxWidth to be the same(as fixed.) 几乎得到特定列并将其setMinWidth和setMaxWidth设置为相同(固定。)

private void fixWidth(final JTable table, final int columnIndex, final int width) {
    TableColumn column = table.getColumnModel().getColumn(columnIndex);
    column.setMinWidth(width);
    column.setMaxWidth(width);
    column.setPreferredWidth(width);
}

Ref: https://forums.oracle.com/thread/1353172 参考: https//forums.oracle.com/thread/1353172

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM