title: use getBaseline(int w, int h) of a child component in the parent container
body: <p>I have a custom swing component that has some child elements, including a JTextField. I want to give the container the same baseline as the JTextField, so I have overridden the getBaseline method in the container. But the position of the JTextField isn't always set when getBaseline is called for the container. I have tried to add a call to doLayout in the container's getBaseline method but that didn't help. What I want is the following:</p>
  
  <pre><code>public int getBaseline(int w, int h) {
      Dimension size = textField.getPreferredSize();
      int textBaseline = textField.getBaseline(size.width, size.height);
      int textY = textField.getY();
      return textY + textBaseline;
  }
  </code></pre>
  
  <p>In the above code textY equals 0.</p>
  
  <p>I've written some code to illustrate the problem. The first time "Add" is clicked the baseline is wrong. The second time it is correct.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.ArrayList;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  class CustomComponent extends JPanel {
  
      public CustomComponent() {
          setLayout(new GridBagLayout());
          textField.setColumns(8);
          layoutElements();
      }
  
      public void addComponent() {
          JPanel comp = new JPanel();
          comp.setPreferredSize(new Dimension(50, 200));
          comp.setBackground(Color.red);
          otherComponents.add(comp);
          layoutElements();
      }
  
      public int getBaseline(int w, int h) {
          Dimension size = textField.getPreferredSize();
          return textField.getY() + textField.getBaseline(size.width, size.height);
      }
  
      public Component.BaselineResizeBehavior getBaselineResizeBehavior() {
          return Component.BaselineResizeBehavior.CONSTANT_DESCENT;
      }
  
      private void layoutElements() {
          removeAll();
          GridBagConstraints constraints = new GridBagConstraints();
          constraints.anchor = GridBagConstraints.SOUTH;
          add(textField, constraints);
  
          for (JPanel comp : otherComponents)
              add(comp, new GridBagConstraints());
          if (getParent() != null)
              getParent().validate();
      }
  
      private JTextField textField = new JTextField();
      private ArrayList&lt;JPanel&gt; otherComponents = new ArrayList&lt;JPanel&gt;();
  }
  
  public class Main {
  
      public static void main(String[] args) {
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
          panel.setLayout(new GridBagLayout());
          frame.getContentPane().add(panel);
  
          JButton addComponent = new JButton("Add");
          GridBagConstraints constraints = new GridBagConstraints();
          constraints.anchor = GridBagConstraints.BASELINE;
          panel.add(addComponent, constraints);
  
          final CustomComponent customComp = new CustomComponent();
          panel.add(customComp, constraints);
  
          addComponent.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent ae) {
                  customComp.addComponent();
              }
          });
  
          frame.setSize(400, 300);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }
  }
  </code></pre>