title: How to properly refresh image in JFrame?
body: <p>This is a problem that disturbs me for few hours now and I'm not able to find a solution by myself...</p>
  
  <p>I've found similar topics all around the net, but I couldn't find exact same problem with well <em>explained</em> and as simple as possible solution. I've also looked at <em>EDT</em> and <em>SwingWorker</em> API docs, but it was far too complicated for me :(</p>
  
  <p>So, let's get to the point. I have a simple JFrame with JLabel inside, that consist of my image:</p>
  
  <pre><code>private static class MyJLabel extends JLabel {
      private ImageIcon img = null;
  
      public MyJLabel(ImageIcon img) {
          super();
          this.img = img;
      }
  
      @Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this);
      }
  }
  
  private static class MyJFrame extends JFrame implements Runnable {
      private BufferedImage img = null;
      private MyJLabel label = null;
  
      public MyJFrame(BufferedImage image, String title) {
          super(title);
          img = image;
      }
  
      @Override
      public void run() {
          Dimension dims = new Dimension(img.getWidth(), img.getHeight());
          dims = new Dimension(dims.width / 2, dims.height / 2);
  
          label = new MyJLabel(new ImageIcon(img));
          label.setPreferredSize(dims);
  
          addComponentListener(new ComponentAdapter() {
              @Override
              public void componentResized(ComponentEvent e) {
                  label.repaint();
              }
          });
          setLayout(new BorderLayout());
          getContentPane().add(BorderLayout.CENTER, label);
          setLocation(200, 200);
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          pack();
          setVisible(true);
      }
  
      public void changeImage(BufferedImage image) {
          img = image;
          if (label != null) {
              label.setIcon(new ImageIcon(img));
              label.repaint();
          }
      }
  }
  </code></pre>
  
  <p>It's invoked by this piece of code:</p>
  
  <pre><code>buffer = receiveImage(in); // download image
  
  MyJFrame f = null;
  javax.swing.SwingUtilities.invokeLater(f = new MyJFrame(buffer, "RDP"));
  
  int x = 0;
  while (x &lt;= 15) {
      txt.println("next"); // notify server that we are ready
  
      while (true) { // wait for server
          if (reader.readLine().equals("ready")) break;
      }
  
      buffer = receiveImage(in); // download image
  
      // do some magic here and refresh image somehow :(
      f.changeImage(buffer); // does not work!
  
      x++;
  }
  </code></pre>
  
  <p>Unfortunately, my approach with <strong>changeImage</strong> method does not work - nothing happens (GUI starts but never gets updated).</p>
  
  <p>I'd appreciate little help with this. <em>Simple</em>, working example with proper explanation would be appreciated the most ;)</p>
  
  <p>Greetings!</p>