aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem in setting the column width of JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in setting the column width of JTable " Watch "problem in setting the column width of JTable " New topic
Author

problem in setting the column width of JTable

aminur rashid
Ranch Hand

Joined: Sep 21, 2001
Posts: 79
I have a JTable = myJavaTable.
It has got two columns.
which i have added to a scroll pane in the following way
myJavaTable.setPreferredScrollableViewportSize(new Dimension(625, 160));
myJavaTable.getColumnModel().getColumn(0).setWidth(12);
But the problem is that the size of both the columns of my table remains same.
How can i fix the initial size of my first column.
Moreover I want that if data in the second column exceeds the column size the horizontal scroll bar should appear.How to do it???


Regards<br />Aminur
Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
That is a problem of the default min size of table columns. Its default minWidth is 15, so if you want to set the width to a value of 12 you first have to set the minWidth to this or a smaller value, for instance setMinWidth(10);
For more details please take a look at swing's source code, class TableColumn, constructor
<code>public TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor)</code>.
In the constructor the minWidth is set to 15.
In the method <code>setWidth(int width)</code> the width is checked against minWidth and maxWidth:
<code>
...
this.width = Math.min(Math.max(width, minWidth), maxWidth);
...
</code>
There you can see that the desired width only is set if width is greater than minWidth and less than maxWidth...
Good luck!
Tom
[This message has been edited by Thomas Suer (edited October 16, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in setting the column width of JTable
 
Similar Threads
JTable Display Problem
JTable
Problem with JTable
JTable
JTable