- 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);
}
}
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);
}
}