Reputation: 1548
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,
Still no luck.
Upvotes: 4
Views: 839
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