• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JScrollPane, spacing and scrolling issues - HELP!

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a solution that works. Instead of JScrollPane containing a JPanel that lays out stuff, I had it contain a JList.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic