title: Java Swing - Cant add scrollbar to my panel
body: <p>Im trying to create a small client where I can paint coordinates onto a map.
  I want to start by loading a map image on to a JPanel and then add that panel to a frame.
  The image is very large hence you need to be able to scroll to see all of it.</p>
  <p>But for some reason my scroll bar wont show up. I tried most guides but it wont work.</p>
  <p>here is my panel class:</p>
  <pre><code>public class MapPanel extends JPanel implements Pointable {
  
      private static final long serialVersionUID = 1L;
      private List&lt;Shape&gt; shapes = new LinkedList&lt;&gt;();
  
  
      public MapPanel() {
          Commander.getInstance().addShapeContainer(this);
          shapeContainerState = NoState.getInstance();
          MouseHandler mouseHandler = new MouseHandler(this);
          KeyListener keyListener = new KeyListener();
  
          this.addMouseListener(mouseHandler);
          this.addMouseMotionListener(mouseHandler);
          this.addKeyListener(keyListener);
          this.setBackground(Color.white);
          this.setFocusable(true);
          this.requestFocusInWindow();
      }
  
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          BufferedImage image;
          try {
              image = ImageIO.read(new File(&quot;earthmap1.jpg&quot;));
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
  
          g.drawImage(image, 0, 0, null);
  }
  </code></pre>
  <p>And here is my JFrame class:</p>
  <pre><code>public class MapFrame extends JFrame {
  
   public MapFrame() {
          buildTheUI();
          setUpEventHandling();
          mapPanel = new MapPanel();
  
          JScrollPane scrollPane = new JScrollPane(mapPanel);
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setSize(900, 800);
          this.add(scrollPane);
          this.setVisible(true);
      }
  }
  </code></pre>
  <p>The image is displayed fine and if i enlarge the window u can see more of it (as all of it is not displayed) but there are no scroll bars so i can see the rest of the image by scrolling.
  <a href="https://i.stack.imgur.com/29DsS.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/29DsS.jpg" alt="enter image description here" /></a></p>