Search code examples
javaswingjframecontentpane

To which part of a Frame does the Menu Bar belongs to in Swing?


The Java documentation claims that the Menu Bar of a Frame is directly positioned and/or attached to the Content Pane of such frame but when I check the source code of this Java classes -specially the one that belongs to the JFrame, JRootPane and Container- it looks to me that the Menu Bar actually belongs to the JRootPane itself, which I know also contains the Content Pane in question but the Menu Bar still doesn't look to be positioned or attacked to the Content Pane itself but the container object which is the RootPane containing the Content Pane.

I'm still new on this so there's a huge chance that I'm just not getting it right or missing something. Any clarification on whether my assessment is accurate or what is it that I'm confusing would be greatly appreciated.

Here's the link where I found that claim: https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html


Solution

  • The Java documentation claims that the Menu Bar of a Frame is directly positioned and/or attached to the Content Pane

    Where does it state that? Don't paraphrase. Post the statement from the tutorial if there is something you don't understand.

    but when I check the source code of this Java classes

    Why would you check the source code when you are just learning Swing?

    it looks to me that the Menu Bar actually belongs to the JRootPane itself,

    And that is exactly what the tutorial says. It states:

    a root pane has four parts

    and goes on to list the content pane and optional menu bar as two of the parts.

    I'm not understanding your confusion?

    You use:

    frame.setJMenuBar(…);
    frame.setContentPane(…)
    

    to add the menu bar and content pane to the frame.