-3

I'm working on a little code. JFrame containing a JPanel with specific dimensions. Here is my code:

import javax.swing.*; 
import java.awt.*; 

public class ScrollPane extends JFrame { 

    public ScrollPane() {
        super("Title");
        setLayout(new BorderLayout());
        setSize(320,240);

        JScrollPane scroller = new JScrollPane(new DrawingPane()); 
        scroller.setPreferredSize(new Dimension(320,240)); 

        add(scroller); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    } 

    private class DrawingPane extends JPanel {
        public DrawingPane(){
            super();
            setSize(640,480);
            setMinimumSize(new Dimension(320,240));
        }

    }

    public static void main(String[] args) {
        new ScrollPane();
    } 
}

Even with a minimum size set for the JPanel, the scrolls don't appear.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Paiku Han
  • 581
  • 2
  • 16
  • 38
  • 1
    `private class DrawingPane extends JPanel { .. setSize(640,480);` I'd replace the `DrawingPane` with a `BufferedImage` displayed in a `JLabel`.. – Andrew Thompson Jul 27 '14 at 19:15
  • +1 for the _workaround_ – Paiku Han Jul 27 '14 at 19:25
  • 1
    After _overriding_ `getPreferredSize()` as pointed out by @camickr (which IMO should be the accepted answer), call `frame.pack()` instead of `frame.setSize()`. The `pack()` method will take all the preffered sizes of internal components into consideration, then size the frame accordingly – Paul Samsotha Jul 28 '14 at 01:32

2 Answers2

5

All components are responsible for determining there preferred size so layout managers can work properly. When doing custom painting you need to override the getPreferredSize() of your custom component to return the Dimension of your component.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
-1

You need to set Preferred Size for your Drawing Panel as well:

private class DrawingPane extends JPanel {
        public DrawingPane(){
            super();
            setPreferredSize(new Dimension(640,480));
            setMinimumSize(new Dimension(320,240));
        }

    }
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • 3
    -1 You should NOT be using the setPreferredSize() method. Instead you should be overriding the getPreferredSize() method. Don't take shortcuts!!! – camickr Jul 27 '14 at 19:27
  • @camickr there are multiple ways of doing things. you might be right but that does not invalidates my answer. – Sanjeev Jul 27 '14 at 19:37
  • 2
    For those that don't want to stick their head in the sand, see [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jul 27 '14 at 20:09
  • @Sanjeev, `there are multiple ways of doing things.` - yes, but the problem is beginners will always take the shortcut, because they think it is easier, not understanding the full implications of the decision. – camickr Jul 27 '14 at 21:06
  • @camickr got your point .. please suggest shall i remove this answer? – Sanjeev Jul 28 '14 at 03:17
  • I would keep it since the answer is accepted. Andrew's link is good reading for those who want to know more. – camickr Jul 28 '14 at 04:19
  • @camickr Now it is not an accepted answer anymore, i am keeping it for Andrew's Link though. thanks – Sanjeev Jul 28 '14 at 05:09