title: scrollbar not showing after call to pack
body: <p>I have a JFrame that contains a JScrollPane which contains a JPanel.
   I upload and draw images on the JPanel. The size of the images is subject to change. Everytime I draw a new image, after an event,  I call pack on the JFrame.
  This all works fine the JFrame and all is correctly resized, but I have problems with the scrollbars which appear and disappear erratically. If I remove the call to pack() the scrollbars are there but the containing JPanel doesn't resize. Why ? What can I do ?
  Here's trimmed SSCCE code for testing. Used images are at
  <a href="https://www.dropbox.com/sh/0vlq13vdi2j626h/uprHdWAiES" rel="nofollow">enter link description here</a></p>
  
  <pre><code>    import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import java.util.ArrayList;
      import javax.imageio.ImageIO;
      import javax.swing.BorderFactory;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JScrollPane;
      import javax.swing.JToolBar;
      import javax.swing.border.Border;
  
      @SuppressWarnings("serial")
      public class JComicFrame extends JFrame {
          private JComicPanel panel;
          private JToolBar toolbar;
          private JButton buttonZoom;
          private JButton buttonPrev;
          private JButton buttonNext;
          private JMenuBar menuBar;
          private JScrollPane scroller;
          private BufferedImage img;
          private ArrayList&lt;BufferedImage&gt; images;
          private int currentFile;
          public JComicFrame(){
  
              super("JComic");        
  
              images = new ArrayList&lt;BufferedImage&gt;();
              try {
                  images.add(ImageIO.read(new File("1.jpg")));
                  images.add(ImageIO.read(new File("2.jpg")));
                  images.add(ImageIO.read(new File("3.jpg")));    
                  images.add(ImageIO.read(new File("4.jpg")));    
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              BorderLayout layout = new BorderLayout();
              setLayout(layout);
              createToolbar();
              createPanel();
              createMenu();
              setJMenuBar(menuBar);
              add(toolbar,BorderLayout.NORTH);
              scroller = new JScrollPane(panel);
              add(scroller,BorderLayout.CENTER);
              setVisible(true);
  
              currentFile = -1;
              BufferedImage img = getNextFile();
              panel.setImage(img);
  
              pack();
  
  
          }
  
          private BufferedImage getNextFile(){
              currentFile = currentFile + 1;
              currentFile = (currentFile &gt; images.size() - 1) ?    images.size() - 1 : currentFile;
              return images.get(currentFile);
          }
  
          private BufferedImage getPrevFile(){
              currentFile = currentFile - 1;
              currentFile = (currentFile &lt; 0) ? 0 : currentFile;
              return images.get(currentFile);
  
          }
  
  
  
          private void createPanel(){
              Border raisedbevel, loweredbevel;
              raisedbevel = BorderFactory.createRaisedBevelBorder();
              loweredbevel = BorderFactory.createLoweredBevelBorder();
              panel = new JComicPanel(img);
              panel.setBorder(BorderFactory.createCompoundBorder(raisedbevel,loweredbevel));
  
  
          }
          private void createToolbar(){
              toolbar = new JToolBar();
              toolbar.setFloatable(false);
              buttonZoom = new JButton("+");
              toolbar.add(buttonZoom);
  
              buttonPrev = new JButton("&lt;-");
              buttonPrev.addActionListener
              (
                      new ActionListener(){
                          public void actionPerformed(ActionEvent e){
                              BufferedImage img = getPrevFile();
                              panel.setImage(img);
                              pack();
                              repaint();
  
  
  
                          }
                      }
                      );
              toolbar.add(buttonPrev);
              buttonNext = new JButton("-&gt;");
              buttonNext.addActionListener
              (
                      new ActionListener(){
                          public void actionPerformed(ActionEvent e){
                              BufferedImage img = getNextFile();
                              panel.setImage(img);
                              pack();
  
  
  
  
                          }
                      }
                      );
              toolbar.add(buttonNext);
              toolbar.setBackground(Color.WHITE);
  
  
          }
          private void createMenu(){
              JMenu menuFile,menuJComic;
              JMenuItem fileOpen; 
              JMenuItem quitJComic,aboutJComic;
  
  
              menuBar = new JMenuBar();
              menuJComic = new JMenu("JComic");
              aboutJComic = new JMenuItem("About JComic...");
              menuJComic.add(aboutJComic);
              quitJComic = new JMenuItem("Quit");
              quitJComic.addActionListener(
                      new ActionListener(){
                          public void actionPerformed(ActionEvent e) {
                              System.exit(0);
                          }
                      }
                      );
              menuJComic.add(quitJComic);
  
              menuBar.add(menuJComic);
  
              menuFile = new JMenu("File");
  
              fileOpen = new JMenuItem("Open...");
  
              menuFile.add(fileOpen);
              menuBar.add(menuFile);
  
  
          }
  
          public static void main(String args[]){
              JComicFrame theFrame = new JComicFrame();
  
  
  
              theFrame.show();
          }
  
      }
  </code></pre>
  
  <p>/////////////////next class///////////////////      </p>
  
  <pre><code>    import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.RenderingHints;
      import java.awt.image.BufferedImage;
  
      import javax.swing.JPanel;
  
  
  
  
  
      public class JComicPanel extends JPanel{
  
  
          private static final long serialVersionUID = 1L;
          private BufferedImage img = null;
          private float scaling = 0.5f;
          private int newW, newH;
  
  
          public JComicPanel(BufferedImage img){
              super();
  
              this.img = img;
  
          }
  
          public JComicPanel(){
              super();
              this.img = null;
  
          }
  
  
          private Runnable scaleImage(BufferedImage img){
              int w = img.getWidth();
              int h = img.getHeight();
              newW = (int)(w * scaling);
              newH = (int)(h * scaling);
              BufferedImage dimg =  new BufferedImage(newW, newH, img.getType());  
              Graphics2D g = dimg.createGraphics();  
              g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
              g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); 
              g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
              this.img = dimg;
              revalidate();
              repaint();
              return null;
  
          }
  
  
          public void setImage(BufferedImage img){
  
              Thread t = new Thread(scaleImage(img));
              t.start();
          }
  
  
          public Dimension getPreferredSize(){
              System.out.println("PS");
              if(img==null || this.isVisible() == false)  
                  return new Dimension(1,1);
              else 
                  return new Dimension(img.getWidth(),img.getHeight());
  
          }
  
          public void paintComponent(Graphics g){
  
  
              Graphics2D g2d = (Graphics2D)g.create();
              super.paintComponent(g);
              g2d.drawImage(img,0,0,getSize().width,getSize().height, this); 
              g2d.dispose();
          }
  
      }
  </code></pre>