Java 自动调整 JTable 中的行高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1783607/
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-12 22:37:34  来源:igfitidea点击:

Auto adjust the height of rows in a JTable

javaswing

提问by AndreaC

In a JTable, how can I make some rows automatically increase height to show the complete multiline text inside? This is how it is displayed at the moment:

在 JTable 中,如何使某些行自动增加高度以显示完整的多行文本?这是它目前的显示方式:

I do not want to set the height for allrows, but only for the ones which have multiline text.

我不想为所有行设置高度,而只想为具有多行文本的行设置高度。

采纳答案by camickr

The only way to know the row height for sure is to render each cell to determine the rendered height. After your table is populated with data you can do:

确定行高的唯一方法是渲染每个单元格以确定渲染高度。在您的表填充数据后,您可以执行以下操作:

private void updateRowHeights()
{
    for (int row = 0; row < table.getRowCount(); row++)
    {
        int rowHeight = table.getRowHeight();

        for (int column = 0; column < table.getColumnCount(); column++)
        {
            Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
            rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
        }

        table.setRowHeight(row, rowHeight);
    }
}

If only the first column can contain multiple line you can optimize the above code for that column only.

如果只有第一列可以包含多行,您可以仅针对该列优化上述代码。

回答by Aaron Digulla

You must iterate over each row, get the bounding box for each element and adjust the height accordingly. There is no code support for this in the standard JTable (see this article for a solutionfor Java ... 1.3.1 =8*O).

您必须遍历每一行,获取每个元素的边界框并相应地调整高度。标准 JTable 中没有对此的代码支持(有关 Java ... 1.3.1 =8*O的解决方案请参阅本文)。

回答by ed22

Camickr's solution did not work for me at all. My data model was dynamic though - it changed all the time. I guess the mentioned solution works for static data, like coming from an array.

Camickr 的解决方案根本不适合我。我的数据模型是动态的——它一直在变化。我猜上述解决方案适用于静态数据,例如来自数组。

I had JPanel for cell renderer component and it's preferred size wasn't set correctly after using prepareRenderer(...). The size was set correctly after the containing window was already visible and did repaint (2 times in fact after some unspecified, though short time). How could I call updateRowHeights() method shown above then and where would I do this? If I called it in (overriden) Table.paint() it obviously caused infinite repaints. It took me 2 days. Literally. The solution that works for me is this one (this is the cell renderer I used for my column):

我有用于单元渲染器组件的 JPanel,并且在使用 prepareRenderer(...) 后它的首选大小设置不正确。在包含窗口已经可见并重新绘制后(实际上在一些未指定的时间后实际上是 2 次,尽管时间很短),大小设置正确。我如何调用上面显示的 updateRowHeights() 方法,我将在哪里执行此操作?如果我在(覆盖)Table.paint() 中调用它,它显然会导致无限重绘。我花了2天。字面上地。对我有用的解决方案是这个(这是我用于我的专栏的单元格渲染器):

public class GlasscubesMessagesTableCellRenderer extends MyJPanelComponent implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        //this method updates GUI components of my JPanel based on the model data
        updateData(value);
        //this sets the component's width to the column width (therwise my JPanel would not properly fill the width - I am not sure if you want this)
        setSize(table.getColumnModel().getColumn(column).getWidth(), (int) getPreferredSize().getHeight());

        //I used to have revalidate() call here, but it has proven redundant

        int height = getHeight();
        // the only thing that prevents infinite cell repaints is this
        // condition
        if (table.getRowHeight(row) != height){
            table.setRowHeight(row, height);
        }
        return this;
    }
}