Menu Appearing Behind Other Components
Hello all,
I am relatively new to Java Swing (a PHP developer with some JSP experience) and I am having some trouble following some code. The original application given in the tutorial does not have a menu bar so I was trying to add one. I've added menu bars to other projects in the past, but for some reason every time I try to add a menu bar using theFrame.setJMenuBar(menuBar), the menu appears but the pop-up menus appear behind other components. Here is the relative code all within a buildGUI() method, all variables are properly scoped and the project compiles fine:
theFrame =new JFrame("Test Application");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout =new BorderLayout();
JPanel background =new JPanel(layout);
background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//setup the menu and add the action listeners
JMenuBar menuBar =new JMenuBar();
JMenu fileMenu =new JMenu("File");
JMenuItem saveMenuItem =new JMenuItem("Save");
JMenuItem newMenuItem =new JMenuItem("New");
JMenuItem loadMenuItem =new JMenuItem("Load");
saveMenuItem.addActionListener(new SaveListener());
loadMenuItem.addActionListener(new LoadListener());
newMenuItem.addActionListener(new NewListener());
fileMenu.add(newMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(loadMenuItem);
menuBar.add(fileMenu);
//snip code adding boxes and checkboxes to panels
background.add(BorderLayout.EAST, buttonBox);
background.add(BorderLayout.WEST, nameBox);
theFrame.getContentPane().add(background);
GridLayout grid =new GridLayout(16,16);
grid.setVgap(1);
grid.setHgap(2);
mainPanel =new JPanel(grid);//create a panel to hold boxes
background.add(BorderLayout.CENTER, mainPanel);//add panel
theFrame.setBounds(50,50,310,310);
theFrame.setJMenuBar(menuBar);
theFrame.pack();
theFrame.setVisible(true);
I could post more but didn't want to make the post stretch out too long. Thanks in advance for your help!
-Kevin
Hello,
Ok, here is the class that holds the GUI code. I removed the actionListeners as they aren't really relevant. The code is supposed to create a frame that holds a panel of buttons on the right, a panel of labels on the left, and a panel of chekboxes (16x16) in the middle. When a user choses start, the checkboxes are looped over and added to the track. I added a menu bar after the initial design and when I click on File it appears behind the panel holding the instrument names. I uploaded a better image now that I am off work (I am stationed in Afghanistan so my schedule is probably the opposite of most of yours).
http://kevineaton.net/problemMenu2.png
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.midi.*;
import java.util.*;
import java.io.*;
public class BeatBox{
//Create the class variables, starting with GUI
JPanel mainPanel;
ArrayList<JCheckBox> checkBoxList;
Sequencer sequencer;
Sequence sequence;
Track track;
JFrame theFrame;
//Store the names of the instruments in an array
String[] instrumentNames = {"Bass Drum","Closed Hi-Hat","Open Hi-Hat","Acoustic Snare","Crash Cymbal","Hand Clap","High Tom","High Bongo","Maracas","Whistle","Low Conga","Cowbell","Vibraslap","Low-mid Tom","High Agogo","Open Hi Conga"};
//Store corresponding drum keys in another array
int[] instruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
//Main actually runs the program by calling the buildGUI method
public static void main(String[] args){
new BeatBox().buildGUI();
}
public void buildGUI(){
theFrame = new JFrame("BeatBox Version 1"); //Create the main frame for holding all of the components
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//make sure it exits
BorderLayout layout = new BorderLayout();
JPanel background = new JPanel(layout); //the panel to hold the other panels
background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//setup the menu and add the action listeners
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem newMenuItem = new JMenuItem("New");
JMenuItem loadMenuItem = new JMenuItem("Load");
saveMenuItem.addActionListener(new SaveListener());
loadMenuItem.addActionListener(new LoadListener());
newMenuItem.addActionListener(new NewListener());
fileMenu.add(newMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(loadMenuItem);
menuBar.add(fileMenu);
//prepare checkboxes for instruments
checkBoxList = new ArrayList<JCheckBox>();
JPanel buttonBox = new JPanel(); //create a panel to hold the buttons on the right
buttonBox.setLayout(new BoxLayout(buttonBox,BoxLayout.Y_AXIS));
//create buttons and assign listeners
JButton start = new JButton("Start");
start.addActionListener(new StartListener());
buttonBox.add(start);//Add the button to the box
JButton stop = new JButton("Stop");
stop.addActionListener(new StopListener());
buttonBox.add(stop);
JButton upTempo = new JButton("Tempo Up");
upTempo.addActionListener(new UpTempoListener());
buttonBox.add(upTempo);
JButton downTempo = new JButton("Tempo Down");
downTempo.addActionListener(new DownTempoListener());
buttonBox.add(downTempo);
JPanel nameBox = new JPanel(); //this panels hold the instrument names on the left
nameBox.setLayout(new BoxLayout(nameBox,BoxLayout.Y_AXIS));
for(int i = 0; i< 16; i++){
nameBox.add(new Label(instrumentNames[i]));
}
GridLayout grid = new GridLayout(16,16);
grid.setVgap(1);
grid.setHgap(2);
mainPanel = new JPanel(grid);//create a panel to hold checkboxes
//now add checkboxes to panel and arraylist
for(int i = 0; i< 256; i++){
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkBoxList.add(c);
mainPanel.add(c);
}
background.add(BorderLayout.CENTER, mainPanel);//add panel to middle background
background.add(BorderLayout.EAST, buttonBox); //add the buttons on the right
background.add(BorderLayout.WEST, nameBox); //add the instrument names on the left
theFrame.getContentPane().add(background); //add the background panel to the frame
setupMidi(); //setup the sequencer and tracks
theFrame.setBounds(50,50,310,310);
theFrame.setJMenuBar(menuBar);//add menubar
theFrame.pack();
theFrame.setVisible(true);
}//exit buildGUI
Thanks for your help everyone!