• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel & JScrollPane  RSS feed

 
jamie ong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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);
setContentPane(jpanel);

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

pack();
setResizable(true);
setSize(730,530);
setVisible(true);
 
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

Rene
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!