This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a JScrollPane in which I want to display a number of rows. Each row must have the same height and of a size that will not change (e.g., 20 pixels). Each row will have the same width and of a size that will be variable throughout the use of the application.
I have a frame that uses a BorderLayout and places the scroll pane in the CENTER. I want the size of the scroll pane to be as big as the CENTER can be.
Now to my problems. At some times given the number of rows and the height each should be, it does not add up to the height of the scroll pane's viewport. In this case, I'm finding it ignores the height of the row and spreads the rows out. At other times given the same thiing, it adds up to more than the height of the scroll pane's viewport. In this case, I'm finding it again ignores the height and packs them together. Basically, when the screen is drawn initially, the scroll pane does what it can do to "fit" my data in its window (expanding or shrinking it). I do not want this. What I need is to retain the number of rows and their height and width regardless of the size of the scroll pane which should be dependent on the size of its container.
I've tried all the different layout managers, nested panels, setting preferred sizes, etc. and all combinations to find nothing works. I'm assuming that since my experience is minimal I am just doing something wrong but I cannot figure out what. I would greatly appreciate any tips or guidance to point me down the right path. Below I've outlined my "current" set of nested widgets I'm trying to use.
The trick is to use a layout manager that will understand and respect the size of the components you hand it. The JScrollPane will ask its view (child component) for the size it needs for display so it can properly set its scrollbars. The view components layout manager computes this size from the size requirements of its containers child components. GridBagLayout respects the preferred size of its child components and will therefore give its parent container, the JScrollPane, an honest accounting of its display needs.