Java 动态自动调整 JTable 列的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6447984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:01:53  来源:igfitidea点击:

Auto resize the widths of JTable's columns dynamically

javajtable

提问by Eng.Fouad

I have a JTable with 3 columns:

我有一个包含 3 列的 JTable:

- No. #
- Name
- PhoneNumber

I want to make specific width for each column as follows:

我想为每列设置特定的宽度,如下所示:

enter image description here

在此处输入图片说明

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. Can you help me solving this issue?

但我没有成功使 myTable 仅在列的当前宽度不适合其内容时动态更新宽度。你能帮我解决这个问题吗?

采纳答案by Eng.Fouad

Here I found my answer: 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.

在这里我找到了我的答案:http: //tips4java.wordpress.com/2008/11/10/table-column-adjuster/
这个想法是检查一些行的内容长度来调整列宽。
在文章中,作者在一个可下载的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 );
}

回答by camickr

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(...);

回答by Alan Dong

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