Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel & JScrollPane

jamie ong
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a JPanel added with images to fit into the JScrollPane. i have set the panel with FlowLayout and preferred size, but the scrollbar only scroll to the preferred size i have set. if i didnt set the preferred size of the panel, the images will run into one straight line, which i dont want. Any ways to fix the problem?
JPanel jimgcontent = new JPanel();
jimgcontent.setLayout(new FlowLayout(FlowLayout.LEFT));
jimgcontent.setPreferredSize(new Dimension(300, 450));
jimgcontent.add(new ImageIcon(filename));
jscrollpane = new JScrollPane(jimgcontent);
jscrollpane.setPreferredSize(new Dimension(700, 400));
jpanel.add(jscrollpane, BorderLayout.CENTER);

/***** screen setting *****/
Dimension windim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((windim.width - 730)/2, (windim.height - 530)/2);

Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jamie,
I think, your problem is the FlowLayout of the panel. You should use GridBagLayout for displaying your images. So you can calculate, where to add the images. It is up to you, but FlowLayout adds Components to the Panel as long as there is enough space available. Because your JPanel is inside of a JScrollPane, there is never ending space. That's why your images are in one line.
Hope this helps

Look ma! I'm selling my stuff!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!