This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Image is always behind the menu (swing) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Image is always behind the menu (swing)" Watch "Image is always behind the menu (swing)" New topic
Author

Image is always behind the menu (swing)

Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
I created a JFrame (swing) with an image, but the JMenu of the Frame is always behind the image.
Please help.
Thx Brain
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try adding a canvas to the layout manager of your JFrame and then adding your image to the canvas. This will force the image to "play nice" with the menu bar in your frame and not try to hog all of the space.
Nick
Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
I tried this :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Try extends JFrame implements Runnable, ActionListener
{
Dimension d;
Toolkit tk = getToolkit();
PCanvas can = new PCanvas();
JPanel panel = new JPanel(new BorderLayout());

public Try(String title)
{
super(title);
d = tk.getScreenSize();
setBounds(d.width/2-300, d.height/2-300, 600, 600);
setResizable(false);
panel.add(new TextField("TEST"));
panel.add(can);
setContentPane(panel);
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
dispose();
System.exit(0);
}
});



}

public void actionPerformed(ActionEvent e)
{

}

public void run()
{

}

public static void main(String[] args)
{
JFrame frame = new Try("Animation");
frame.show();
}

}

class PCanvas extends Canvas
{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawLine(1,1,20,20);
}
}
But know the textfield will be hide.
Sorry, I'm a very beginner...
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Your code looks good, except that you haven't created and attached a JMenuBar object to the frame. You will have to create an instance of JMenuBar and add some JMenuBarItem's to it. Then call setJMenuBar() on your JFrame.
Nick
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I just compiled an example and I finally understood what you were saying. The problem is a result of mixing Lightweight (swing) components with heavyweight (awt) ones. If you are using JFrame and a JMenuBar together with a Canvas, you will see the JMenuBar pop up behind the Canvas. There are two ways to fix this. One way is to simply use a JPanel instead of a Canvas for your painting surface. It will work the same but is lightweight and won't cause this problem. The other way will only work for JPopupMenu objects, so for a JMenuBar, use the solution I just gave. For JPopupMenu objects there is a method called setDefaultLightWeightPopupEnabled() that will fix the problem.
Finally, I hope this helps. Sorry for my misunderstanding.
Nick
 
jQuery in Action, 2nd edition
 
subject: Image is always behind the menu (swing)