Programming: Java Help needed

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
I'm trying to display a bunch of thumbnail images (JButtons with ImageIcons) in a JPanel in a JScrollPane. The problem is that it puts all the images on one row instead of starting a new row when it gets to the right edge of the screen. So basically all the images are on one row and I have to scroll left to right to see them all. Basically, it thinks there is an infinate amount of space in the JPanel because it is in a JScrollPane (even if i disable the horizontal scrollbar). Before I added the JScrollPane (and just added my JPanel directly to the JFrame) I didn't have this problem. It would put the Images on a new row once it ran out of room on a row. However, I need a vertical scrollbar so I need a JScrollPane.

Can anyone help? I think what I need is a way to link the size of the JPanel to the size of the window.

Here is a short program that illustrates my problem. The 20 labels all go on one line, going off the edge of the screen, instead of going on a new line.

import javax.swing.*;

public class ScrollPaneProblem extends JFrame {
public static void main(String args[]) {
ScrollPaneProblem spp = new ScrollPaneProblem();
JPanel jp = new JPanel();
JScrollPane sp = new JScrollPane(jp,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
spp.add(sp);

JLabel[] jl = new JLabel[20];
for (int i=0; i<20; i++) {
jl = new JLabel("<!!!!!!!!!!!!!!!!!!!!!!!!!!!!!>");
jp.add(jl);
}

spp.setDefaultCloseOperation(EXIT_ON_CLOSE);
spp.setSize(600,400);
spp.setVisible(true);
}
}
 

SinNisTeR

Diamond Member
Jan 3, 2001
3,570
0
0
what about making a gridlayout? having it be however many columns by rows. then you can populate the grid using a loop.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Try giving your JScrollPane a preferred size:

setPreferredSize(Dimension d);
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
The problem with that is I want it to be resizeable, so if the windows is small there might be only 4 images per row, but if it's bigger there might be say 10 images per row. FlowLayout is perfect for this, and it works great if I don't put it in a scroll pane.
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Originally posted by: lozina
Try giving your JScrollPane a preferred size:

setPreferredSize(Dimension d);


I've tried setting PreferredSize and MaximumSize of both the scrollpane and panel and it doesn't work.
 

znaps

Senior member
Jan 15, 2004
414
0
0
Set the panel's preferred size, and don't set any other sizes. That should work fine.
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Originally posted by: znaps
Set the panel's preferred size, and don't set any other sizes. That should work fine.

It doesn't work, try it with my example. I think it's because the scroll pane overrides the panels size even if you set preferred size. I've done some googling on this and a lot of people have been having this problem (flowlayout and jscrollpanel don't work well together). Haven't found a good solution, some people have gone as far as write a new layout manager.
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Ok I tried using setPreferredSize again and this time it worked, so I guess I was doing something wrong before. However, now I have a new problem. If I set the prefered size to a really low number (width or height), it sets it to the size of the window/scrollpane which is fine for the width. The problem is, if I set it to a high number, it sets it to exactly that size no matter how much stuff I've added to the panel.

For example, if I add enough buttons to use up a 3000 pixel high panel but set preferred height to 2000, I can only scroll through the first 2000 pixels. The rest of the buttons are inaccessible. If the preferred height is set to 4000, the scroll bar will be too long and will go to a blank area past the buttons.

I know I could make some code that would change the preferred height whenever the window is resized, based on the size of the window and number/size of buttons, but there must be a better way than that. Is there any way to get the preferred height to automatically adjust so that it is always just enough to scroll through all the buttons?

Thanks very much for everyones help so far.