title: Updating JLabel Dynamically causes GUI to distort - Works after resizing window
body: <p>I have a GUI. The GUI is a JFrame that has a Panel[Gridbaglayout]. This gridbag layout has 3 different components. The middle component is a panel[GridBagLayout] which has the JLabel that I'm talking about. It also contains a JScrollBar that I am taking values from and updating the JLabel with as the user moves the bar.</p>
  
  <p>The code I'm using to get the value and update the JLabel:</p>
  
  <pre><code>public class DrinkAdjustmentListener implements AdjustmentListener{
  
      @Override
      public void adjustmentValueChanged(AdjustmentEvent e) {
          drinkLabel.setText("Percentage " + e.getValue() + "%");
      }
  }
  </code></pre>
  
  <p>I understand when coding for Android, the main Thread is the UI Thread as well. With Swing I don't believe that's the case and I'm not sure really how to properly update the GUI. Is this fine and it's something else that's causing the distortion, perhaps the layout manager?</p>
  
  <p>Before:</p>
  
  <p><a href="https://i.stack.imgur.com/tQYJR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tQYJR.png" alt="Before"></a></p>
  
  <p>After:</p>
  
  <p><a href="https://i.stack.imgur.com/pqc0d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pqc0d.png" alt="After"></a></p>
  
  <p>This is an some example code to run demonstrating what I was trying to achieve. Surprisingly it worked. I will have to make a longer example to exploit the problem.</p>
  
  <pre><code>public class Gui {
  
  private JLabel jLabel;
  
  public void displayGui(){
  
      JFrame jFrame = new JFrame();
      jFrame.setSize(500,500);
  
      JPanel mainPanel = new JPanel(new GridBagLayout());
      mainPanel.setPreferredSize(new Dimension(400,400));
  
      jLabel = new JLabel("Some Percentage 0%");
  
      GridBagConstraints c = new GridBagConstraints();
      c.gridx = 0;
      c.gridy = 0;
  
      mainPanel.add(jLabel,c);
  
      JScrollBar jScrollBar = new JScrollBar();
      jScrollBar.addAdjustmentListener(new MyAdjustmentListener());
  
      c = new GridBagConstraints();
      c.gridx = 0;
      c.gridy = 1;
  
      mainPanel.add(jScrollBar,c);
  
      jFrame.add(mainPanel);
  
      jFrame.pack();
      jFrame.setVisible(true);
  }
  
  public class MyAdjustmentListener implements AdjustmentListener{
  
      @Override
      public void adjustmentValueChanged(AdjustmentEvent e) {
          jLabel.setText("Some Percentage " + e.getValue() + "%");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>EDIT 8/15/2017, 11:30AM:
  I found a workaround. I figured that since when I resize the window it seemed to repaint itself and looked correct. I just put jFrame.repaint() after every time setText would be called in the AdjustmentListener. As a side note it looked as if the entire gui was getting redrawn inside the "Options JPanel" as seen in the picture.</p>