title: stop resizing jpanel after user resize
body: <p>I have a JPanel with a JLabel which frequently loads a new picture. I programmed it to resize to fit the picture after every load. However, I would for the user to be able to adjust the panel to a certain size and leave it that way (with current reloads the inability to permanently resize can be aggravating). My code doesn't work though:</p>
  
  <pre><code>public class Grapher extends JFrame{        
      private JScrollPane scrollPane;
      private static JPanel topPanel;
      private static JLabel label;
      private static ImageIcon image;
  
      private static boolean dontResize = false;
      private static boolean firstTime = true;
  
      public Grapher(){
          setTitle( "some graph" );
          setBackground( Color.gray );
          setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  
          topPanel = new JPanel();
          topPanel.setLayout( new BorderLayout() );
          getContentPane().add( topPanel );
          label = new JLabel();
  
          scrollPane = new JScrollPane();
          scrollPane.getViewport().add( label );
          topPanel.add( scrollPane, BorderLayout.CENTER );
          topPanel.addComponentListener(new Listeners());
          topPanel.setName("Soar Grapher Panel");
      }
  
      public void display(String fileName){
          //steal focus the first time an image is displayed
          if(image == null)
              setVisible(true);
          image = new ImageIcon(fileName);
          image.getImage().flush();
          boolean temp = dontResize;
          setSize(getPreferredSize());
          dontResize = temp;
          System.out.println("afterWards: " + dontResize);
          label.setIcon(image);
          label.repaint();
      }
  
      @Override
      public Dimension getPreferredSize(){
          if(dontResize){
              System.err.println("not resizing!");
              return getSize();
          }
          if (image == null)
              return new Dimension(100,100);
          else
              return new Dimension(image.getIconWidth() &gt; getWidth() + 40 ? image.getIconWidth() + 40 : getWidth(),
                      image.getIconHeight() &gt; getHeight() + 60 ? image.getIconHeight() + 60 : getHeight());
      }
  
      private class Listeners implements ComponentListener{
  
              @Override
              public void componentHidden(ComponentEvent arg0) {}
  
              @Override
              public void componentMoved(ComponentEvent arg0) {}
  
              @Override
              public void componentResized(ComponentEvent arg0) {
                  System.out.println("componentResized: " + dontResize);
                  //TODO make it not resize after the user resizes it themselves.
                  System.err.println(arg0.getComponent().getName());
                  System.err.println("resized!");
                  if(!firstTime)
                      dontResize = true;
                  firstTime = false;
              }
  
              @Override
              public void componentShown(ComponentEvent arg0) {}
      }
  
  }
  </code></pre>
  
  <p>I'm attempting to save the dontResize boolean when the program resizes, but unfortunately the listener seems to activate before the <code>resize()</code> function does. How do I differentiate user resizing from programmatic resizing?</p>
  
  <p>EDIT_<strong><em>_</em>__<em>_</em>__</strong>
  Removing a listener during the duration of the programmatic resizing seems to be the way to go; however, the listener seems to be called at random times besides the user resizing, messing everything up. This is what I have:</p>
  
  <pre><code>private static ComponentListener listeners = new Listeners();
  private static boolean resizeOkay = true;
  ...
  [inside the display() function]
      if(resizeOkay){
          removeComponentListener(listeners);
          System.err.println("No handler should be called between here");
          setSize(getPreferredSize());
          System.err.println("and here.");
          addComponentListener(listeners);
      }
      label.setIcon(image);
      label.repaint();
  [inside the listener]
  @Override
  public void componentResized(ComponentEvent arg0) {
      System.err.println("resize handler");
      resizeOkay = false;
  }
  </code></pre>
  
  <p>Without any user resizing, the above code prints:</p>
  
  <pre><code>No handler should be called between here
  and here.
  resize handler
  resize handler
  </code></pre>
  
  <p>How else is it being called? This prevents the windows from ever being resized.</p>