Jump to content

TableColumn's method setWidth doesn't work

- - - - -

  • Please log in to reply
2 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Hi, i need some help froum you again. I try to create jtable with 5 columns. first four columns should have the same width and the right column must be wider. I use TableColumn's method setWidth, but it does not work. Here's my code:

public class Sizes
{
   ...
    public static final int ROW_HEIGHT = 30;
    
    public static final int TRADES_HISTORY_TABLE_WIDTH = 680;
    public static final int TRADES_HISTORY_TABLE_HEIGHT = 400;
    public static final int TRADES_HISTORY_TABLE_COLUMN_WIDTH = 120;
    public static final int TRADES_HISTORY_TABLE_RIGHT_COLUMN_WIDTH = 200;
    public static final int TRADES_HISTORY_TABLE_COLUMNS_COUNT = 5;
    ...
}
    private static JTable formatTable(JTable table, int columnsCount, 
            int columnWidth, int rightColumnWidth)
    {
        //table.setVisible(true);
        table.setRowHeight(Sizes.ROW_HEIGHT);
        table.setSize(Sizes.TRADES_HISTORY_TABLE_WIDTH, Sizes.TRADES_HISTORY_TABLE_HEIGHT);
        
        DefaultTableCellRenderer alignmentRenderer = new DefaultTableCellRenderer();
        alignmentRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        for (int i = 0; i < columnsCount; i++)
        {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setCellRenderer(alignmentRenderer);
            if (i == 4)
                column.setWidth(rightColumnWidth);
            else
                column.setWidth(rightColumnWidth);
        }
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        return table;
    }


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
First, instead of using TableColumn.setWidth(), you should use TableColumn.setPreferredWidth().

(From JDK 6 Docs: [url=http://download.oracle.com/javase/6/docs/api/javax/swing/table/TableColumn.html#setWidth(int)]TableColumn.setWidth(int)[/url]:


"[i]Like a layout manager in the AWT, the JTable adjusts a column's width automatically whenever the table itself changes size, or a column's preferred width is changed. Setting widths programmatically therefore has no long term effect.[/i]"

And your problem is probably this:
            if (i == 4)

                column.setWidth(rightColumnWidth);

            else

                column.setWidth(rightColumnWidth);
Both conditions result in the same evaluation. Whether or not i == 4, column.setWidth() will be called with rightColumnWidth. You probably wanted to do this:
            if (i == 4)

                column.setPreferredWidth(rightColumnWidth);

            else

                column.setPreferredWidth(columnWidth);

Wow I changed my sig!

#3
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Thanks, setPreferredSize was what i needed




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users