• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in setting the column width of JTable

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic