This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JTable setMinimumSize problem  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable, which will auto-adjust its column widths based on the data in the cells. And to make this work I have to turn auto resize off by calling table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

The table sits in a JPanel with certain width.

The problem is that with auto_resize_off, when initially there is no data in the table or when the data in the grid is not long enough, the table will not expand across the entire width of the JPanel. So there is an empty gap between the right-end of the table and the right edge of the JPanel.
So I tried to do table.setMinimumSize(new Dimension(MyJPanelWidth, whateverHeight)) to make the initial width of the table same as that of the JPanel. But it does not work! Nothing gets changed for the table width.

However, table.setPreferredSize will change the table width, but my table will not auto-expand its columns, which does not serve my purpose either.

So my question is -- how do I set the initial/minimum table width without impacting its ability to auto-adjust its columns based on data?

Any advice is much appreciated!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iterate the columns, setting width for each



where [width] could be the preferredSize of the scrollPane or panel,
divided by the columnCount
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!