• Post Reply Bookmark Topic Watch Topic
  • New Topic

Column width in JTable  RSS feed

 
Burk Hufnagel
Ranch Hand
Posts: 839
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's an old issue, I think, but I have yet to find a reasonable answer.
I've got a JTable with several columns. I want to set the preferred width of the column such that you can see all the data, but not so wide that there's wasted space.
The problem is that I'm doing a database access to get the contents of the fields (all Strings BTW) and while I know the length of each field in the database, I *don't* know how wide the widest String is going to be when it's displayed.
I'd like to reset the width at runtime when the JTable's model is updated with new values, but I really don't like the idea of iterating through each cell, getting a stringWidth() and using the max value. Seems way too processor intensive.
I was thinking that there should be some industry accepted way to use the length of the database field and a String filled with M's or something like that to do the calculation, but I haven't found anything like that.
Any suggestions?
Thanks!
Burk
Each of the columns should be set
 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max value doesn't sound too processing intensive too me. How many values would you have to iterate through?
 
Burk Hufnagel
Ranch Hand
Posts: 839
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is a "sample" application - for the SCJD exam - so there's only 24 rows in the database and a row has nine columns, so you're right.
The problem is that I would like an answer to use in the "real world" - and, where I work, databases are measured in terrabytes.
So, I'm still looking for a reasonable and useful answer...
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
you can query the database for the longest string in the specific column. though I don't think java is too slow to compute the longest string of some thousands. another way would be some average/guessed value, that sets the column wide enough for most cases. as preferred size does not lock the column, the user can always increase the width if necessary.
chantal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!