title: Dynamically resize JTextField in JTree Node
body: <p>I am using a JPanel with some JLabels and JTextFields as editor and renderer in a JTree.</p>
  
  <p>The user can click on any JTextField and modify the text. All works fine.</p>
  
  <p>All I am missing is how I can dynamically resize (grow) the JTextField as the user is typing.</p>
  
  <p>Below a small sample program that illustrates the problem. Click on i.e. "colors" and enter some additional characters. The JTextField keeps its size, which is expected, but I would like it to grow and shrink to accommodate the text.</p>
  
  <p>I tried different layout managers, calculate the preferredSize of the JTextField and the JPanel, call invalidate() and the JPanel and the Tree and so on, without success.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>import java.awt.Component;
  import java.util.EventObject;
  
  import javax.swing.AbstractCellEditor;
  import javax.swing.BoxLayout;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.JTree;
  import javax.swing.tree.TreeCellEditor;
  import javax.swing.tree.TreeCellRenderer;
  
  public class Tree {
  
      public static void main(final String[] args) {
          new Tree().grow();
      }
  
      private void grow() {
          final JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          final JTree tree = new JTree();
          tree.setEditable(true);
          tree.setCellRenderer(new SomeTreeCellRenderer());
          tree.setCellEditor(new SomeTreeCellRenderer());
  
          frame.add(new JScrollPane(tree));
  
          frame.pack();
          frame.setSize(400, 300);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
  
      private class SomeTreeCellRenderer extends AbstractCellEditor implements 
              TreeCellRenderer, TreeCellEditor {
  
          @Override
          public Object getCellEditorValue() {
              return null;
          }
  
          @Override
          public boolean isCellEditable(final EventObject event) {
              return true;
          }
  
          @Override
          public Component getTreeCellRendererComponent(
                  final JTree tree, final Object value, final boolean selected, final boolean expanded, 
                  final boolean leaf, final int row, final boolean hasFocus) {
  
              final JPanel panel = new JPanel();
              final BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
              panel.setLayout(layout);
  
              panel.add(new JLabel("Some text: "));
              panel.add(new JTextArea(String.valueOf(value)));
  
              return panel;
          }
  
          @Override
          public Component getTreeCellEditorComponent(
                  final JTree tree, final Object value, final boolean isSelected,
                  final boolean expanded, final boolean leaf, final int row) {
  
              return getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, false);
          }
  
      }
  
  }
  </code></pre>