开发者

Calling setRowHeight on JTable immediately resets int method argument

I am having trouble understanding why this might be happening. I have been searching online for a similar problem but couldn't find one.

for ( int row = 0; row < getRowCount(); row++ ) {
        int rowHeight = this.getRowHeight();
        for ( int col = 0; col < this.getColumnCount(); col++ ) {
           Component pRenderer = getCellRenderer( row, col );
        // Assume this is a dimension of 120x120
        Dimension pRendererSize = pRenderer.getPreferredSize();
           if ( pRendererSize.height  > rowHeight )
            rowHeight = pRendererSize.height;
        }
     this.setRowHeight( row, rowHeight );
}

The default height for a row in my JTable is 16 pixels. If I make the call:

//This returns 16 initially
this.getRowHeight();

I am stepping through the code in my debugger. At the line setRowHeight the argument rowHeight is 120 thus I am setting the rowHeight of my row to 120 pixels. I did not overload this method at all, this is the JTable implementation of setRowHeight.

What happens next is truly frightening. After stepping over this method, not only does this.getRowHeight() still return 16 pixels, the value of rowHeight immediately changes to 16 pixels.

Of course the table renders incorrectly too. This seems to defy sense that my int value type argument would change by passing it to a method. This should not change the rowHeight variable at all. Do you think my debugger is ru开发者_Python百科nning on old code, is broken or could this be the end of times?


Here is the code I use to set row heights:

private void updateRowHeights()
{
    try
    {
        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);
        }
    }
    catch(ClassCastException e) {}
}

UIManager Defaults uses this code. Click on "by Value Type" and then select "Icon" to see it working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜