Component contentPane = layeredPane.getComponent(0); Component titlePane = layeredPane.getComponent(1); Component menuBar = null; if (layeredPane.getComponentCount() > 2 && layeredPane.getComponent(2) instanceof JMenuBar) menuBar = layeredPane.getComponent(2);
Component contentPane = rp.getContentPane(); Component menuBar = rp.getJMenuBar();
    public Dimension preferredLayoutSize(Container parent)    {      JRootPane rp = (JRootPane) parent;      JLayeredPane layeredPane = rp.getLayeredPane();      Component contentPane = layeredPane.getComponent(0);      Component titlePane = layeredPane.getComponent(1);      Component menuBar = null;      if (layeredPane.getComponentCount() > 2          && layeredPane.getComponent(2) instanceof JMenuBar)        menuBar = layeredPane.getComponent(2);      // We must synchronize here, otherwise we cannot guarantee that the      // prefSize is still non-null when returning.      synchronized (this)        {          if (prefSize == null)            {              Insets i = parent.getInsets();              prefSize = new Dimension(i.left + i.right, i.top + i.bottom);              Dimension contentPrefSize = contentPane.getPreferredSize();              prefSize.width += contentPrefSize.width;              prefSize.height += contentPrefSize.height                                 + titlePane.getPreferredSize().height;              if (menuBar != null)                {                  Dimension menuBarSize = menuBar.getPreferredSize();                  if (menuBarSize.width > contentPrefSize.width)                    prefSize.width += menuBarSize.width - contentPrefSize.width;                  prefSize.height += menuBarSize.height;                }            }          // Return a copy here so the cached value won't get trashed by some          // other component.          return new Dimension(prefSize);      }    }