File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JMenuBar menu displaying below a component on container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenuBar menu displaying below a component on container" Watch "JMenuBar menu displaying below a component on container" New topic
Author

JMenuBar menu displaying below a component on container

Daniel Devlin
Greenhorn

Joined: Sep 05, 2011
Posts: 15
Hi all,
I'm reusing code to create an image processing application. When I select file from my file menu the list of options is appearing below the originalContainer. This is a JPanel that contains a canvas onto which buffered images are drawn. Could someone please help explain why the menu is being drawn before the canvas is it appears the canvas attached to the JPanel is the problem.

public ImageLoader(){
super("IRIS Application");
JMenu fileMenu = new JMenu("File");

JMenuItem fileOpenChoice = new JMenuItem("Open File");
fileOpenChoice.addActionListener(new FileOpener());
fileMenu.add(fileOpenChoice);

JMenuItem fileSaveChoice = new JMenuItem("Save File");
fileSaveChoice.addActionListener(new FileSaver());
fileMenu.add(fileSaveChoice);

JMenu processMenu = new JMenu("Process");

JMenuItem reduceContrast = new JMenuItem("Reduce Contrast");
reduceContrast.addActionListener(new ReduceContrast());
processMenu.add(reduceContrast);

JMenuItem scale = new JMenuItem("Scale");
scale.addActionListener(new Scaler());
processMenu.add(scale);

JMenuItem sobel = new JMenuItem("Sobel");
sobel.addActionListener(new Sobel());
processMenu.add(sobel);

JMenuItem prewitt = new JMenuItem("Prewitt");
prewitt.addActionListener(new Prewitt());
processMenu.add(prewitt);

originalImage = new BufferedImage(565, 584, BufferedImage.TYPE_BYTE_GRAY);
processedImage = new BufferedImage(565, 584, BufferedImage.TYPE_BYTE_GRAY);
originalRaster = originalImage.getRaster();
processedRaster = processedImage.getRaster();
ic = new ImageCanvas();
pc = new ImageCanvas();

Container content = this.getContentPane();
content.setLayout(new FlowLayout());
JPanel originalContainer = new JPanel();
originalContainer.add(ic);
originalContainer.setSize(ic.getWidth(), ic.getHeight());
originalContainer.setBorder(new EtchedBorder());
JPanel processedContainer = new JPanel();
processedContainer.add(pc);
processedContainer.setSize(pc.getWidth(), pc.getHeight());
processedContainer.setBorder(new EtchedBorder());
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new FlowLayout());
imagePanel.add(originalContainer);
imagePanel.add(processedContainer);
//content.add(imagePanel, BorderLayout.NORTH);
JPanel originalGrid = new JPanel();
originalGrid.setLayout(new GridLayout(0,1,5,0));
JPanel processedGrid = new JPanel();
processedGrid.setLayout(new GridLayout(0,1,5,0));
originalText = new JTextField("Original Text");
JButton originalButton = new JButton("View Coordinates Table");
originalGrid.add(originalText);
originalGrid.add(originalButton);
JTextField processedText = new JTextField("Processed Text");
JButton processedButton = new JButton("View Coordinates Table");
processedGrid.add(processedText);
processedGrid.add(processedButton);
JPanel informationPanel = new JPanel();
informationPanel.setBackground(Color.red);
informationPanel.setLayout(new BorderLayout());
informationPanel.add(originalGrid, BorderLayout.WEST);
informationPanel.add(processedGrid, BorderLayout.EAST);
//content.add(informationPanel, BorderLayout.SOUTH);
ic.addMouseMotionListener(new ImageMouseMotionListener(ic, originalText));
pc.addMouseMotionListener(new ImageMouseMotionListener(pc, processedText));
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
bar.add(processMenu);

content.add(imagePanel, BorderLayout.NORTH);
content.add(informationPanel, BorderLayout.SOUTH);
setJMenuBar(bar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 725);
setResizable(false);
setVisible(true);
setLocation(125, 0);
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the name suggests this line is the likely culprit
ic = new ImageCanvas();

if ImageCanvas is an awt.Canvas, you're mixing awt components (heavyweight) with swing components (lightweight)
heavyweight components will be drawn above lightweight components.

instead of awt.Canvas, use a JPanel
 
Consider Paul's rocket mass heater.
 
subject: JMenuBar menu displaying below a component on container