• Post Reply Bookmark Topic Watch Topic
  • New Topic

JMenuBar covers content pane  RSS feed

 
Brian Johnsonmyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my GUI, JMenuBar covers the top of my content pane in my JFrame. As a result, if I draw a line from (0,0) to (100,100), for example, the top part of the line is obscured. I fixed this by creating my own JPanel content pane and adding it to the JFrame. When I draw the same line in the paint() of my custom content pane, the line is drawn correctly, i.e., is not covered by the menu bar.

Is it necessary for me to use my own custom content pane, or am I not using the default content pane correctly?
 
Rob Spoor
Sheriff
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either Darryl Burke or Rob Camick (or both) would say you shouldn't paint on a JFrame directly. So yes, letting the new content pane* do the painting would be a better option.

* You can either add a custom JPanel to the existing content pane, or use setContentPane to overwrite the current one. It doesn't really matter much which option you take.
 
Brian Johnsonmyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Johnsonmyer wrote:In my GUI, JMenuBar covers the top of my content pane in my JFrame.

Nope. It does however cover the top part of the root pane and layered pane.

More details from the JRootPane API:
The custom LayoutManager used by JRootPane ensures that:
  • The glassPane fills the entire viewable area of the JRootPane (bounds - insets).
  • The layeredPane fills the entire viewable area of the JRootPane. (bounds - insets)
  • The menuBar is positioned at the upper edge of the layeredPane.
  • The contentPane fills the entire viewable area, minus the menuBar, if present.
  •  
    Brian Johnsonmyer
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For the benefit of others, I think my confusion was caused by the paint(). As I understand it, both the JFrame and the content pane can a have paint(). My line from (0,0) to (100,100) was being covered by JMenu because it was being drawn by paint() in JFrame, not by paint() in the content pane.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 6981
    110
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Unrelated to your issue, but important: Don't override paint() in Swing! Override paintComponent() instead.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!