JMenuItems drawn over by JPanel. How to fix?

Hi.

When i add my menu to the frame the actual menu bar displays fine. but when you open the menu the items are drawn over by my jpanel.

I tried a way to work around it by adding the menu to the jpanel. when i did this the menu worked and displayed fine, but it wasn't really wanted (it wasn't being mounted to the top of the frame like the title bar, but instead a component centered on the canvas).

I want the menu bar to be positioned like any other gui application and use my jpanel for the drawing area. is there some way to let java know i want to display the menu over the jpanel?

Heres the basic structure of the code i'm using:

publicclass Simulateextends JAppletimplements MouseMotionListener, MouseListener{

staticprivate JFrame frame;

private Container content;

private Display drawing_surface;

private JMenuBar menu_bar;

publicstaticvoid main(String []args){

init_frame();

}

public Simulate(){

drawing_surface=new Display();

setContentPane(drawing_surface);

content=getContentPane();

/* add menu items */

}

staticvoid init_frame(){

frame=new JFrame();

frame.getContentPane().add(new Simulate());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class Displayextends JPanel{

publicvoid paintComponent(Graphics g){

paint_buffer(g);

}

publicvoid paint_buffer(Graphics g){

/* paint here */

}

}

[2827 byte] By [xplagu3a] at [2007-11-26 20:41:47]
# 1

Swing related questions should be posted in the Swing forum.

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html]How to Use Menus[/url] for a working example.

A JApplet is a top level container you don't add one top level container to another. See the tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/components.html]Swing Components[/url].

Also you should be invoking super.paintComponent(...) in the paintComponent() method. Again, see the tutorial from above for the section on "Custom Painting".

camickra at 2007-7-10 2:00:43 > top of Java-index,Java Essentials,Java Programming...
# 2
drawing_surface=new Display();setContentPane(drawing_surface);content=getContentPane();?
DrLaszloJamfa at 2007-7-10 2:00:43 > top of Java-index,Java Essentials,Java Programming...
# 3
static private JFrame frame;Why make that component, and that component alone, static?
DrLaszloJamfa at 2007-7-10 2:00:43 > top of Java-index,Java Essentials,Java Programming...
# 4
You have to make it static if you wish to point to that frame in other methods (such as owner of JDialog). Maybe there is a smarter way?
Icycoola at 2007-7-10 2:00:43 > top of Java-index,Java Essentials,Java Programming...
# 5
> You have to make it static if you wish to point to> that frame in other methods (such as owner of> JDialog). Maybe there is a smarter way?Any number of smarter ways. The code has a bad smell now.
DrLaszloJamfa at 2007-7-10 2:00:43 > top of Java-index,Java Essentials,Java Programming...