Java.awt.Canvas eclipses drop down JMenu in GUI

First, thanks to anyone who can offer me assistance on this topic. The problem is something that I feel should

not have occurred, and i also feel that there should be a simple solution that just seems oblivious to me right now. Perhaps I am using the wrong components or initializing them incorrectly; I am not sure because GUI programming is not one of my strong points.

Below is the sample application code generated from Netbeans GUI Builder (Project Matisse) for my JFrame called "NewApplication". I've added the canvas that I would like to use to the JFrame. The main problem is that the java.awt.Canvas component eclipses/covers up the JMenu drop down component. I am not sure how to avoid this. Clearly, the Menu should be painted above anything else within the frame but for some reason this is not the case.

package RPG.Floor;

publicclass NewApplicationextends javax.swing.JFrame{

public NewApplication(){

initComponents();

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

canvas1 =new java.awt.Canvas();

menuBar =new javax.swing.JMenuBar();

fileMenu =new javax.swing.JMenu();

openMenuItem =new javax.swing.JMenuItem();

saveMenuItem =new javax.swing.JMenuItem();

saveAsMenuItem =new javax.swing.JMenuItem();

exitMenuItem =new javax.swing.JMenuItem();

editMenu =new javax.swing.JMenu();

cutMenuItem =new javax.swing.JMenuItem();

copyMenuItem =new javax.swing.JMenuItem();

pasteMenuItem =new javax.swing.JMenuItem();

deleteMenuItem =new javax.swing.JMenuItem();

helpMenu =new javax.swing.JMenu();

contentsMenuItem =new javax.swing.JMenuItem();

aboutMenuItem =new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

canvas1.setBackground(java.awt.Color.white);

fileMenu.setText("File");

openMenuItem.setText("Open");

fileMenu.add(openMenuItem);

saveMenuItem.setText("Save");

fileMenu.add(saveMenuItem);

saveAsMenuItem.setText("Save As ...");

fileMenu.add(saveAsMenuItem);

exitMenuItem.setText("Exit");

exitMenuItem.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

exitMenuItemActionPerformed(evt);

}

});

fileMenu.add(exitMenuItem);

menuBar.add(fileMenu);

editMenu.setText("Edit");

cutMenuItem.setText("Cut");

editMenu.add(cutMenuItem);

copyMenuItem.setText("Copy");

editMenu.add(copyMenuItem);

pasteMenuItem.setText("Paste");

editMenu.add(pasteMenuItem);

deleteMenuItem.setText("Delete");

editMenu.add(deleteMenuItem);

menuBar.add(editMenu);

helpMenu.setText("Help");

contentsMenuItem.setText("Contents");

helpMenu.add(contentsMenuItem);

aboutMenuItem.setText("About");

helpMenu.add(aboutMenuItem);

menuBar.add(helpMenu);

setJMenuBar(menuBar);

org.jdesktop.layout.GroupLayout layout =new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(canvas1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(canvas1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}// </editor-fold>

privatevoid exitMenuItemActionPerformed(java.awt.event.ActionEvent evt){

System.exit(0);

}

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new NewApplication().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JMenuItem aboutMenuItem;

private java.awt.Canvas canvas1;

private javax.swing.JMenuItem contentsMenuItem;

private javax.swing.JMenuItem copyMenuItem;

private javax.swing.JMenuItem cutMenuItem;

private javax.swing.JMenuItem deleteMenuItem;

private javax.swing.JMenu editMenu;

private javax.swing.JMenuItem exitMenuItem;

private javax.swing.JMenu fileMenu;

private javax.swing.JMenu helpMenu;

private javax.swing.JMenuBar menuBar;

private javax.swing.JMenuItem openMenuItem;

private javax.swing.JMenuItem pasteMenuItem;

private javax.swing.JMenuItem saveAsMenuItem;

private javax.swing.JMenuItem saveMenuItem;

// End of variables declaration

}

[7611 byte] By [OrangeKyoa] at [2007-11-27 9:05:01]
# 1
This is why you're not supposed to mix Swing and AWT components. Use a JPanel instead.
uncle_alicea at 2007-7-12 21:38:45 > top of Java-index,Desktop,Core GUI APIs...
# 2

I've seen many examples and tutorials that mix AWT and swing components. I had no idea that it was ill advised.

However, the goal of my project is to provide a basic, tiled map. Users should be able to select certain tiles and click to arrange them on the canvas (or now JPanel) as they please.

Will the JPanel component allow a complete control of its paint area such that I could draw arbitrary graphics?

Will an implementation of the MouseListener interface on a class that extends the JPanel component allow for event handling, for the various mouse click events?

OrangeKyoa at 2007-7-12 21:38:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

Throughout the Swing API you'll see Component or Container listed as arguments or return types, but that's mainly for backward compatibility. In practice, the runtime types of the referenced objects should virtually always be subclasses of JComponent. If you see a tutorial that tells you to mix Canvas and JMenu, throw that tutorial away.

As for the rest of your questions: try it and see. ^_^

uncle_alicea at 2007-7-12 21:38:45 > top of Java-index,Desktop,Core GUI APIs...
# 4
k thx.
OrangeKyoa at 2007-7-12 21:38:45 > top of Java-index,Desktop,Core GUI APIs...