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