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?
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.
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?
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.