user3041058
user3041058

Reputation: 1548

JScrollpane not scrolling big content

I have this simple example where I want to display a large panel in a small frame. Java documentation tell the scrollpane will automatically fit its contents in its viewport and add scrollbars. However I have no such luck. No scrollpane nor its content appear. Here is what I have tried.

public class MyWindow extends JFrame {
    JPanel panel;
    JScrollPane scroll;

    public MyWindow(){  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        panel.setSize(1000,1000);
        scroll = new JScrollPane(panel);

        add(scroll,BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);
    }
}

As stated in similar questions,

  1. I made the panel bigger than frame.
  2. I added border layout to frame.

Still no luck.

Upvotes: 4

Views: 839

Answers (1)

Reimeus
Reimeus

Reputation: 159844

The JScrollPane is free to disregard the initial size of the panel. You need to set the preferred size

panel = new JPanel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1000, 1000);
    };
};

Upvotes: 5

Related Questions