• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image is always behind the menu (swing)

 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic