This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Dynamic JTable column sizing with horizontal toolbar

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Did anyone manage to dynamically size a JTable and at the same time allows a horizontal scrollbar to appear when the gui window's size is reduced.
I managed to size the table dynamically by following this post, but whenever i reduce the size of the gui window, the columns get squeezed together, making the fields impossible to read. I would like to have a horizontal scrollbar so that when the window size is reduced, the values can be read using the scrollbar.
The only way i know to make the horizontal scrollbar appear is to set table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF) but for dynamic resizing, i need to have table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN) at the end of the setupTable() method. They seem to contradict one another. So is it impossible to have both scrollbar and dynamic re-sizing?
Another disadvantage of table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF) is when you maximize the gui window, the table does not fill the whole window, leaving some undesirable gray section at the right hand side.
Anyone who had managed, please give some hints.
thanks,
derek
[ January 16, 2004: Message edited by: Derek Canaan ]
[ January 16, 2004: Message edited by: Derek Canaan ]
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
"Did anyone manage to dynamically size a JTable and at the same time allows a horizontal scrollbar to appear when the gui window's size is reduced."
When I first started out on this project, I began by writing a file verifier to familiarize
myself with the file i/o. I then wrote up the first draft of my user interface in swing.
It's been quite a while since I've even looked at my user interface, as this project has
turned out to involve steep learning curves for me in topics I knew little about. This
morning I ran my application's alone client:
java -jar runme.jar alone
and, my JTable appears to do what you want, although, the JTable has no data in it
beyond the header information (name, address, zip, et cetera).
The JTable has no data, beyond header columns, and yet the JTable has space used
in the body (maybe I placed blanks in there so that the JTable would not have zero
rows?).
When I have more rows than can fit in the space the layout manager allows, a
vertical scroll bar will appear for the JTable. When I width gets to small,
horizontal scroll bars appear. Never is the column width unreasonably and
surprisingly too small.
So, when my window is small, there are vertical and horizontal scrollbars, and the
columns do not scrunch together, but remain reasonably far apart. And, when
I expand the window to fill the complete screen (1024 x 768), the columns even
become wider.
In general, I do not attempt to resize individual columns, I am using the tools and
techniques a beginner would use, and my JTable appears to work quite fine; perhaps
I got lucky. Perhaps I referenced the lucky book, or perhaps I just took the elementary
advice offered at Sun in their swing tutorial.
What I think I did, and I have not referred to the code explicitly just now, is that within
my GUI, the JTable is in the center of a JPanel border layout; and, this JPanel is
within that gizmo which creates your scroll bars, JScrollBar... or some such. And,
JScrollBar is probably within the center of the master panel for the window's border
layout.
Anyway, without having looked at my code, that's probably how I did it. I kept everything
simple, and, of course, I wanted the JTable to always expand to take maximum width,
without the columns getting scrunched together.
When the window is very small, the JScrollBar... gizmo must be smart enough to say,
"okay, let's not scrunch up these columns, let's introduce scroll bars now."
I'm sorry that I have not coded up a solution, and I'm sorry that I have not taken
the time to review my coding solution, but I suspect that as I have outlined it
above is probably, in general, how I went about it.
I also seem to remember things not working correctly the first time, so I had to
iterate a few times before I lucked out and got my JTable to behave in a reasonable
fashion.
Thanks,
Javini Javono
 
Derek Canaan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Javini,
Thanks for your input.

my JTable appears to work quite fine


Can your columns fit in all its field values when you reduce the window frame size? What if one column contains a very long field value? Do you have to manually 'lengthen' its columns to see what is inside?
The idea of dynamic re-sizing is to have the program automatically re-sizes the columns whatever their max field length is so that the user do not have to 'lengthen' or open-up the column to see the entire value when the field value is long.
The idea of horizontal scrollbar is to help view when the sum of the column lengths is greater than the window frame width.
If JTable.AUTO_RESIZE_OFF is used, what about the empty spaces when the sum of the column lengths is lesser than the window frame width - not very nice.
Actually, i have found a way (using a listener) but not sure if it's the most efficient or necessary
rgds,
derek
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic