title: Java Swing - JLabel width changed when icon or text added?
body: <p><strong>Same Question, different context</strong></p>
  
  <p>It seems I was too hasty in my accepting before, since the problem is still there.  The problem? JLabel takes the liberty of expanding its parent panel when content is added to it.</p>
  
  <p>It's time for reproducing it per "Hovercraft full of eels"-ses suggestion, and here it is:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  
  public class TestLabel {    
      public static void main(String[] args) {
      // Var inits
          JFrame frame;
      JPanel panel;
      JLabel label;
      Container pane;
      GridBagConstraints gbc = new GridBagConstraints();
  
      // Frame, content pane, layout inits
      frame = new JFrame("Label Tester");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
  
          pane = frame.getContentPane();   
          pane.setLayout(new GridBagLayout());
  
          gbc.weighty = 1;
          gbc.fill = GridBagConstraints.BOTH;
  
  
          // Add panels (note gbc weighty and fill carries over all instances)
          gbc.weightx = 0.3;
          gbc.gridx = 0;
          gbc.gridy = 0;
          panel = new JPanel();
          panel.setBackground(Color.GREEN);
          frame.add(panel,gbc);
  
          label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING");
          //label = new JLabel("");
          label.setOpaque(true);
          label.setBackground(Color.WHITE);   
          panel.add(label);
  
          gbc.weightx = 0.7;
          gbc.gridx = 1;
          gbc.gridy = 0;
          panel = new JPanel();
          panel.setBackground(Color.RED);
          frame.add(panel,gbc);
  
          gbc.weightx = 0.3;
          gbc.gridx = 0;
          gbc.gridy = 1;
          panel = new JPanel();
          panel.setBackground(Color.BLUE);
          frame.add(panel,gbc);
  
          gbc.weightx = 0.7;
          gbc.gridx = 1;
          gbc.gridy = 1;
          panel = new JPanel();
          panel.setBackground(Color.YELLOW);
          frame.add(panel,gbc);
  
          frame.pack();
          frame.setSize(800,600);  
  
          frame.setVisible(true);  
      }
  }
  </code></pre>
  
  <p>Results:</p>
  
  <p><img src="https://i.stack.imgur.com/BppSv.png" alt="enter image description here"></p>
  
  <p>As you can see, the green panel is forced wider and throws off my whole layout when text (or, in the original question, and icon) is added to it.  I want my layout to remain the same weights, regardless of the content. The reason this came up is because I'm trying to add a scaled image as an icon to the label, as seen in the original question. </p>
  
  <p>Incidentally, <code>setPreferredSize()</code> doesn't seem to work.</p>
  
  <p>Is there a way to fix this?</p>
  
  <hr>
  
  <p><em>Original Question</em></p>
  
  <p>My JLabel element expands dramatically when I add an Icon to it.  Why is this happening?  Here's the applicable portion of the code:</p>
  
  <pre><code>// Show label and BG color
  redLabel.setBackground(Color.RED);
  redLabel.setOpaque(true);
  
  // Grab stretched image (already loaded elsewhere in the code) and turn to icon
  Img = Img.getScaledInstance(redLabel.getWidth(),12,Image.SCALE_REPLICATE);
  ImageIcon icon = new ImageIcon(Img); 
  
  // This line throws everything off! 
  //It's commented out in the first pic, and included in the second.
  redLabel.setIcon(icon);
  </code></pre>
  
  <p>As you can see from the first pic, I've got a label (in red) of width W.  What I'm trying to do is stretch my icon to width W and put it in the label.</p>
  
  <p>When I do this, the label expands (by exactly 50 pixels, I think) and also squeezes over the left edge (green).  Does anyone have any idea why this is happening?</p>
  
  <p>I've tried several things that are too verbose to explain but can't find the problem :-/</p>
  
  <p><img src="https://i.stack.imgur.com/dOcwG.png" alt="enter image description here"></p>