0

Actually i am adding a Jlayeredpane to a Jscrollpane and then a Jlabel is again added to the Jlayeredpane. I am seeting the size of jlabel explicitly as jlayeredpane has null layout, In the Jlabel i am setting an image.

But the problem is that the scrollbar is not shown here.Please help in the design flaw or guide me to other alternatives.

Tech Enthusiast
  • 279
  • 1
  • 5
  • 18
  • 1
    for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, shows your issue with PreferredSize for Layer(null layout) – mKorbel Oct 28 '13 at 13:03
  • ImageIcon icon = new ImageIcon("output1.jpg"); imageview.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); imageview.setIcon(icon); // imageview is the Jlabel here // and i am adding the jlayeredpane to jscrollpane – Tech Enthusiast Oct 28 '13 at 13:03
  • @Mohit: add the code to your question. – Holger Oct 28 '13 at 13:10
  • got it and solved by setting preferredsize. Thanks mkorbel – Tech Enthusiast Oct 28 '13 at 13:11
  • 1
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Oct 28 '13 at 13:16

1 Answers1

0

its may be you expected. but don't use null layout. JScrollPane not work with null layout.

import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollLabel extends JFrame {

    public ScrollLabel() throws Exception {
        setSize(300, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel(new GridLayout());
        pane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        label = new JLabel();
        ImageIcon icon = new ImageIcon(ImageIO.read(new File(getClass().getResource("baby.jpg").toURI())));
        label.setIcon(icon);
        panel.add(label);
        add(pane);
        repaint();
    }

    public static void main(String[] args) throws Exception {
        new ScrollLabel();
    }
    JPanel panel;
    JLabel label;
    JScrollPane pane;
}
subash
  • 3,116
  • 3
  • 18
  • 22