Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JMenuBar menu displaying below a component on container

 
Daniel Devlin
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic