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 void layoutContainer(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);      Component glassPane = rp.getGlassPane();      if (glassPaneBounds == null || layeredPaneBounds == null          || contentPaneBounds == null || menuBarBounds == null)        {          Insets i = rp.getInsets();          int containerWidth = parent.getBounds().width - i.left - i.right;          int containerHeight = parent.getBounds().height - i.top - i.bottom;          // 1. The glassPane fills entire viewable region (bounds - insets).          // 2. The layeredPane filles entire viewable region.          // 3. The titlePane is placed at the upper edge of the layeredPane.          // 4. The menuBar is positioned at the upper edge of layeredPane.          // 5. The contentPane fills viewable region minus menuBar minus          //    titlePane, if present.                // +-------------------------------+          // |  JLayeredPane                 |          // |  +--------------------------+ |          // |  | titlePane                + |          // |  +--------------------------+ |          // |  +--------------------------+ |          // |  | menuBar                  | |          // |  +--------------------------+ |          // |  +--------------------------+ |          // |  |contentPane               | |          // |  |                          | |          // |  |                          | |          // |  |                          | |          // |  +--------------------------+ |          // +-------------------------------+          // Setup titlePaneBounds.          if (titlePaneBounds == null)            titlePaneBounds = new Rectangle();          titlePaneBounds.width = containerWidth;          titlePaneBounds.height = titlePane.getPreferredSize().height;          // Setup menuBarBounds.          if (menuBarBounds == null)            menuBarBounds = new Rectangle();          menuBarBounds.setBounds(0,                                  titlePaneBounds.y + titlePaneBounds.height,                                  containerWidth, 0);          if (menuBar != null)            {              Dimension menuBarSize = menuBar.getPreferredSize();              if (menuBarSize.height > containerHeight)                menuBarBounds.height = containerHeight;              else                menuBarBounds.height = menuBarSize.height;            }          // Setup contentPaneBounds.          if (contentPaneBounds == null)            contentPaneBounds = new Rectangle();          contentPaneBounds.setBounds(0,                                      menuBarBounds.y + menuBarBounds.height,                                      containerWidth,                                      containerHeight - menuBarBounds.y                                      - menuBarBounds.height);          glassPaneBounds = new Rectangle(i.left, i.top, containerWidth, containerHeight);          layeredPaneBounds = new Rectangle(i.left, i.top, containerWidth, containerHeight);        }      // Layout components.      glassPane.setBounds(glassPaneBounds);      layeredPane.setBounds(layeredPaneBounds);      if (menuBar != null)        menuBar.setBounds(menuBarBounds);      contentPane.setBounds(contentPaneBounds);      titlePane.setBounds(titlePaneBounds);    }