title: How to display buffered image in JLabel or JPanel?
body: <p>I have this <code>JFrame</code> with <code>JLabel label1</code> but I cant make it display the image. How do I display the updated buffered image in the label every time the <code>showScreenShot()</code> is called by another class?</p>
  
  <p>I'm using Eclipse WindowBuilder. </p>
  
  <p>Here's the code:</p>
  
  <pre><code>public class TeacherDashBoard extends JFrame {
  
      private JPanel contentPane;
      private BufferedImage img;
      private static TeacherDashBoard frame;
      private static JLabel label1;
  
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      frame = new TeacherDashBoard();
                      frame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      public void showScreenShot(byte[] bytes) throws IOException {
          img = ImageIO.read(new ByteArrayInputStream(bytes));
          label1.getGraphics().drawImage(img, 0, 0, label1.getWidth(), label1.getHeight(), null);
      }
  
      public TeacherDashBoard() {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setBounds(100, 100, 450, 424);
          contentPane = new JPanel();
          contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
          setContentPane(contentPane);
          contentPane.setLayout(null);
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          label1 = new JLabel();
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
              .addGap(29)
              .addComponent(label1, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
              .addContainerGap(591, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
              .addGap(96)
              .addComponent(label1, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
              .addContainerGap(333, Short.MAX_VALUE))
          );
          getContentPane().setLayout(layout);
  
          pack();
      }
  }
  </code></pre>