title: JPanel fills entire JScrollPane
body: <p>I'm making a level editor for a project of mine so I added a JPanel to a JScrollPane in order to view the level being worked on.  The idea was that if the level was too big the scroll pane would handle that.  The issue is when the level is to small, when I try to size the panel to a size smaller than the scrollpane it gets stretched to fill the whole scrollpane instead of the size I set.  I can't seem to find a solution to the problem.</p>
  
  <p>EDIT: I added a some sample code, When the window is enlarged the panel stretches to fit the scrollpane instead of staying at the set 500 by 500 in the example.</p>
  
  <p>Setting up the JScrollPane:</p>
  
  <pre><code>public class Example {
  
  public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(600, 600);
  
          LevelView levelView = new LevelView();
          JScrollPane pane = new JScrollPane(levelView);
  
          frame.add(pane, BorderLayout.CENTER);
          frame.setVisible(true);
      }
  
  }
  </code></pre>
  
  <p>JPanel:</p>
  
  <pre><code>public class LevelView extends JPanel {
  
      private static final long serialVersionUID = 1L;
  
      public LevelView() {
          Dimension dim = new Dimension(500, 500);
          setPreferredSize(dim);
          setMaximumSize(dim);
          setMinimumSize(dim);
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          System.out.println(getWidth() + " " + getHeight());
          g.setColor(Color.BLACK);
          g.fillRect(0, 0, getWidth(), getHeight());
  
  
          // Draw grid
          g.setColor(Color.WHITE);
          for (int x = 0; x &lt; getWidth(); x += 50) {
              g.drawLine(x, 0, x, getHeight());
          }
          for (int y = 0; y &lt; getHeight(); y += 50) {
              g.drawLine(0, y, getWidth(), y);
          }
  
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance!</p>