aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane, spacing and scrolling issues - HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane, spacing and scrolling issues - HELP!" Watch "JScrollPane, spacing and scrolling issues - HELP!" New topic
Author

JScrollPane, spacing and scrolling issues - HELP!

Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
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.

container.setLayout(new BorderLayout());
container.add(new MyTopPanel(), BorderLayout.NORTH);
container.add(new MyBottomScrollPane(), BorderLayout.CENTER);

MyBottomScrollPane - extends JScrollPane
setViewportView(new MyScrollPaneContent());

MyScrollPaneContent - extends JPanel
setLayout(???); // I need a bunch of rows
for (int i = 0; i < 10; i++) {
add(new MyRowContent());
}

MyRowContent - extends Canvas
width = calculated
height = always 21 pixels


Many, MANY thanks to any who can help!
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
I found a solution that works. Instead of JScrollPane containing a JPanel that lays out stuff, I had it contain a JList.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane, spacing and scrolling issues - HELP!