File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Canvas hiding away menubar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Canvas hiding away menubar" Watch "Canvas hiding away menubar" New topic
Author

Canvas hiding away menubar

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hi folks,

I'm pretty new to Swing (having done a little prototyping just for fun every couple of years...) and stumbled into a problem while putting up a simple "image editor".

The problem is that if I load a Canvas into the main window's (JFrame with BorderLayout) container, the menus from the menubar don't show up at all. If I put something like a JPanel or JLabel to replace the Canvas, the menus work just fine. Should I make the Canvas be somehow "deeper" on the Z-axis or what?

Thanks.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What you should do is just use a JPanel. This is the whole "mixing heavy and light weight component" issue. Canvas is an AWT component. If you absolutly have to use a canvas object there is a method you can call on your JMenuBar but I can't think of it off the top of my head. I am looking at the API right now and will tell you as soon as I find it.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, the method I was looking for was setLightweightPopupEnabled(boolean) but it isn't availabel for a JMenuBar. I found it with JComboBox. So you really should just use a JPanel anyway. Any reason you can't?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The following article provides some ideas on how to solve such problems:
java.sun.com: Mixing heavy and light components


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Lasse Koskela:
Should I make the Canvas be somehow "deeper" on the Z-axis or what?


Canvas is "real" in that there's an OS-level component that corresponds to it, while the JMenus and other Swing components are actually just drawn on the JFrame by Java. Therefore it's actually impossible for a Canvas to be "behind" any Swing components that are in the same container. As Gregg says, the solution is to use a JPanel instead of a Canvas.

To answer your next question before you ask it: when you subclass JPanel, don't override paint(). Instead, override paintComponent() and call super.paintComponent() as the first line in your override.


[Jess in Action][AskingGoodQuestions]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Thanks guys! I'll get back to you when I've tried the JPanel stuff.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Canvas hiding away menubar
 
Similar Threads
How to disable Menubar
Menu
menu behaviour when resizing windows in Swing
Canvas + JMenu
JScrollPane and JMenuBar are incompatible?