• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Canvas hiding away menubar

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following article provides some ideas on how to solve such problems:
java.sun.com: Mixing heavy and light components
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! I'll get back to you when I've tried the JPanel stuff.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic