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???
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).]
subject: problem in setting the column width of JTable