title: Resize Swing component when content has changed dynamically
body: <p>I have, the same issue with two components JTextField and JComboBox, I assume the solution I'm looking for would solve it for all components.</p>
  
  <p>I have set the size of the components to default thus their size fits the initial content I supplied to it. when I change the content of a component to exceeds the region of the component, I cannot see the whole text, and I would like my component to resize to fit the text.</p>
  
  <p>How can I accomplish that?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The pack() on the frame only enlarged the text field, how can I do the same and enlarge the combo box?</p>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
      private static final long serialVersionUID = 752379460716217273L;
      Dimension maxSize=new Dimension();
      @Override
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
          setText(value.toString());
          Dimension size = getPreferredSize();
          if(maxSize.width&lt;size.width)
              maxSize.width=size.width;
          if(maxSize.height&lt;size.height)
              maxSize.height=size.height;
  
          resolutionDescriptor_ComboBox.setPreferredSize(maxSize);
          return this;
      }
  
  }
  </code></pre>
  
  <p>this works, not very efficient, but it is a first step, thing is, it does not take the button image into size considerations, so some of the text is still not shown, but the component resizes, do you have any suggestions? </p>
  
  <p>Adam.</p>
  
  <p>Answer:</p>
  
  <p>This did the trick together with a pack(), no revalidation needed.</p>
  
  <pre><code>    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
      private static final long serialVersionUID = 752379460716217273L;
      Dimension maxSize=new Dimension();
      @Override
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
          setText(value.toString());
          Dimension size = getPreferredSize();
          if(maxSize.width&lt;size.width) {
              maxSize.width=size.width;
              resolutionDescriptor_ComboBox.setPrototypeDisplayValue(value.toString());
          }
          if(maxSize.height&lt;size.height)
              maxSize.height=size.height;
  
          return this;
      }
  
  }
  </code></pre>
  
  <p>make sure you design something more efficient then this...</p>
  
  <p><strong>Update:</strong></p>
  
  <p>and there is no need for the pack()!</p>
  
  <p>Adam.</p>