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 replies to this topic
#1
Posted 06 February 2011 - 09:45 AM
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:
|
|
|
#2
Posted 06 February 2011 - 09:55 AM
First, instead of using TableColumn.setWidth(), you should use TableColumn.setPreferredWidth().
And your problem is probably this:
(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
Posted 07 February 2011 - 04:53 PM
Thanks, setPreferredSize was what i needed
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









