I have a Jframe with a menu and some submenus Menu Submenu1 Submenu2 Submenu3
I then create 3 containers in the frame using GridBagLayout to position them Container one is JPanel Container one is JTextAreal Container one is Jpanel
When I make my menu selection the part of the Submenu that goes through the first Jpanel masked by the Jpanel.
How do I get the menu selections to be displayed on top of the frames and not behind them
code beneath
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass MainWindow extends JFrame implements WindowListener, ActionListener {
private JMenuBar menuBar = new JMenuBar(); // Window menu bar
String newline = "\n";
String[] fileItems = new String[] { "New", "Save", "Print", "Exit" };
String[] unitItems = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
char[] fileShortcuts = { 'N','S','P','X' };
int startTimeMinute = 0;
int startTimeSecond = 0;
int endTimeMinute = 50;
int endTimeSecond = 9;
private JPanel startDisplay;
private JTextArea dataIn; //text panel
private Panel controlPanel; //Button control panel
private Button openButton, closeButton, markButton;
public MainWindow() {
super("PTC DATA REDUCTION");
addWindowListener(this);
/***** Select Layout Manager Interface *****/
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
/***** Sets up main screen data areas *****/
try {
// controlls the message data area on the main display
startDisplay = new JPanel ();
startDisplay.setBackground(Color.yellow);
startDisplay.setLayout(new FlowLayout(FlowLayout.LEFT));
Label messageLabel = new Label(" ");
Font fontHev9 = new Font("Helvetica", Font.PLAIN, 12);
startDisplay.setFont(fontHev9);
startDisplay.add(messageLabel, "Left");
addComponent (contentPane, startDisplay, 0, 0,
GridBagConstraints.REMAINDER, 1,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,
1.0, 0.0);
// controlls the data text area on the main display
dataIn = new JTextArea(20,20);
dataIn.setBackground(Color.green);
addComponent (contentPane, dataIn, 0,
GridBagConstraints.RELATIVE,
GridBagConstraints.REMAINDER, 1,
GridBagConstraints.BOTH,
GridBagConstraints.EAST, 1.0, 1.0);
// controlls the Portfolio Buttons area on the main display
controlPanel = new Panel();
controlPanel.setLayout (new FlowLayout());
controlPanel.setBackground(Color.pink);
openButton = new Button("Open Port");
openButton.addActionListener(this);
controlPanel.add(openButton);
openButton.setEnabled(false);
closeButton = new Button("Close Port");
closeButton.addActionListener(this);
controlPanel.add(closeButton);
closeButton.setEnabled(false);
markButton = new Button("Mark");
markButton.addActionListener(this);
controlPanel.add(markButton);
markButton.setEnabled(false);
addComponent (contentPane, controlPanel, 0,
GridBagConstraints.RELATIVE,
GridBagConstraints.REMAINDER, 1,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.SOUTHWEST, 1.0, 0.0);
}
// Catch AWT exception
catch (Exception e) {
System.out.println("MainWindow: problem with AWT: ");
e.printStackTrace();
}
updateDisplay();
/*
* Menu begin
*/
setJMenuBar(menuBar);
/*
* Makes File menu and Submenu
*/
JMenu fileMenu = new JMenu("File");
JMenu startMenu = new JMenu("Start Time");
JMenu startMinSubMenu = new JMenu("Minutes");
JMenu startSecSubMenu = new JMenu("Seconds");
JMenu endMenu = new JMenu("End Time");
JMenu endMinSubMenu = new JMenu("Minutes");
JMenu endSecSubMenu = new JMenu("Seconds");
JMenu helpMenu = new JMenu("Help");
/*
* Assemble the File menus with keyboard accelerators
*/
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
item.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts[i],
java.awt.Event.CTRL_MASK, false));
item.addActionListener(this);
fileMenu.add(item);
}
/*
* Assemble the submenus of the Start Time Menu for minutes.
* Register a listener for the radio buttons.
*/
ButtonGroup startMinTypes = new ButtonGroup();
JMenu start00Min = new JMenu("00");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item00Unit = new JRadioButtonMenuItem(unitItems[x]);
item00Unit.addActionListener(this);
start00Min.add(item00Unit);
startMinTypes.add(item00Unit);
item00Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeMinute = Integer.parseInt(event.getActionCommand());
updateDisplay();
}
});
}
JMenu start10Min = new JMenu("10");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item10Unit = new JRadioButtonMenuItem(unitItems[x]);
item10Unit.addActionListener(this);
start10Min.add(item10Unit);
startMinTypes.add(item10Unit);
item10Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeMinute = Integer.parseInt(event.getActionCommand()) + 10;
updateDisplay();
}
});
}
JMenu start20Min = new JMenu("20");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item20Unit = new JRadioButtonMenuItem(unitItems[x]);
item20Unit.addActionListener(this);
start20Min.add(item20Unit);
startMinTypes.add(item20Unit);
item20Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeMinute = Integer.parseInt(event.getActionCommand()) + 20;
updateDisplay();
}
});
}
JMenu start30Min = new JMenu("30");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item30Unit = new JRadioButtonMenuItem(unitItems[x]);
item30Unit.addActionListener(this);
start30Min.add(item30Unit);
startMinTypes.add(item30Unit);
item30Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeMinute = Integer.parseInt(event.getActionCommand()) + 30;
updateDisplay();
}
});
}
JMenu start40Min = new JMenu("40");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item40Unit = new JRadioButtonMenuItem(unitItems[x]);
item40Unit.addActionListener(this);
start40Min.add(item40Unit);
startMinTypes.add(item40Unit);
item40Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeMinute = Integer.parseInt(event.getActionCommand()) + 40;
updateDisplay();
}
});
}
JMenu start50Min = new JMenu("50");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item50Unit = new JRadioButtonMenuItem(unitItems[x]);
item50Unit.addActionListener(this);
start50Min.add(item50Unit);
startMinTypes.add(item50Unit);
item50Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeMinute = Integer.parseInt(event.getActionCommand()) + 50;
updateDisplay();
}
});
}
startMinSubMenu.add(start00Min);
startMinSubMenu.add(start10Min);
startMinSubMenu.add(start20Min);
startMinSubMenu.add(start30Min);
startMinSubMenu.add(start40Min);
startMinSubMenu.add(start50Min);
startMenu.add(startMinSubMenu);
/*
* Assemble the submenus of the Start Time Menu for seconds.
* Register a listener for the radio buttons.
*/
ButtonGroup startSecTypes = new ButtonGroup();
JMenu start00Sec = new JMenu("00");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item00Unit = new JRadioButtonMenuItem(unitItems[x]);
item00Unit.addActionListener(this);
start00Sec.add(item00Unit);
startSecTypes.add(item00Unit);
item00Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeSecond = Integer.parseInt(event.getActionCommand());
updateDisplay();
}
});
}
JMenu start10Sec = new JMenu("10");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item10Unit = new JRadioButtonMenuItem(unitItems[x]);
item10Unit.addActionListener(this);
start10Sec.add(item10Unit);
startSecTypes.add(item10Unit);
item10Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeSecond = Integer.parseInt(event.getActionCommand()) + 10;
updateDisplay();
}
});
}
JMenu start20Sec = new JMenu("20");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item20Unit = new JRadioButtonMenuItem(unitItems[x]);
item20Unit.addActionListener(this);
start20Sec.add(item20Unit);
startSecTypes.add(item20Unit);
item20Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeSecond = Integer.parseInt(event.getActionCommand()) + 20;
updateDisplay();
}
});
}
JMenu start30Sec = new JMenu("30");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item30Unit = new JRadioButtonMenuItem(unitItems[x]);
item30Unit.addActionListener(this);
start30Sec.add(item30Unit);
startSecTypes.add(item30Unit);
item30Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeSecond = Integer.parseInt(event.getActionCommand()) + 30;
updateDisplay();
}
});
}
JMenu start40Sec = new JMenu("40");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item40Unit = new JRadioButtonMenuItem(unitItems[x]);
item40Unit.addActionListener(this);
start40Sec.add(item40Unit);
startSecTypes.add(item40Unit);
item40Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeSecond = Integer.parseInt(event.getActionCommand()) + 40;
updateDisplay();
}
});
}
JMenu start50Sec = new JMenu("50");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item50Unit = new JRadioButtonMenuItem(unitItems[x]);
item50Unit.addActionListener(this);
start50Sec.add(item50Unit);
startSecTypes.add(item50Unit);
item50Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
startTimeSecond = Integer.parseInt(event.getActionCommand()) + 50;
updateDisplay();
}
});
}
startSecSubMenu.add(start00Sec);
startSecSubMenu.add(start10Sec);
startSecSubMenu.add(start20Sec);
startSecSubMenu.add(start30Sec);
startSecSubMenu.add(start40Sec);
startSecSubMenu.add(start50Sec);
startMenu.add(startSecSubMenu);
/*
* Assemble the submenus of the End Time Menu for minutes.
* Register a listener for the radio buttons.
*/
ButtonGroup endMinTypes = new ButtonGroup();
JMenu end00Min = new JMenu("00");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item00Unit = new JRadioButtonMenuItem(unitItems[x]);
item00Unit.addActionListener(this);
end00Min.add(item00Unit);
endMinTypes.add(item00Unit);
item00Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeMinute = Integer.parseInt(event.getActionCommand());
updateDisplay();
}
});
}
JMenu end10Min = new JMenu("10");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item10Unit = new JRadioButtonMenuItem(unitItems[x]);
item10Unit.addActionListener(this);
end10Min.add(item10Unit);
endMinTypes.add(item10Unit);
item10Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeMinute = Integer.parseInt(event.getActionCommand()) + 10;
updateDisplay();
}
});
}
JMenu end20Min = new JMenu("20");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item20Unit = new JRadioButtonMenuItem(unitItems[x]);
item20Unit.addActionListener(this);
end20Min.add(item20Unit);
endMinTypes.add(item20Unit);
item20Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeMinute = Integer.parseInt(event.getActionCommand()) + 20;
updateDisplay();
}
});
}
JMenu end30Min = new JMenu("30");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item30Unit = new JRadioButtonMenuItem(unitItems[x]);
item30Unit.addActionListener(this);
end30Min.add(item30Unit);
endMinTypes.add(item30Unit);
item30Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeMinute = Integer.parseInt(event.getActionCommand()) + 30;
updateDisplay();
}
});
}
JMenu end40Min = new JMenu("40");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item40Unit = new JRadioButtonMenuItem(unitItems[x]);
item40Unit.addActionListener(this);
end40Min.add(item40Unit);
endMinTypes.add(item40Unit);
item40Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeMinute = Integer.parseInt(event.getActionCommand()) + 40;
updateDisplay();
}
});
}
JMenu end50Min = new JMenu("50");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item50Unit = new JRadioButtonMenuItem(unitItems[x]);
item50Unit.addActionListener(this);
end50Min.add(item50Unit);
endMinTypes.add(item50Unit);
item50Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeMinute = Integer.parseInt(event.getActionCommand()) + 50;
updateDisplay();
}
});
}
endMinSubMenu.add(end00Min);
endMinSubMenu.add(end10Min);
endMinSubMenu.add(end20Min);
endMinSubMenu.add(end30Min);
endMinSubMenu.add(end40Min);
endMinSubMenu.add(end50Min);
endMenu.add(endMinSubMenu);
/*
* Assemble the submenus of the End Time Menu for seconds.
* Register a listener for the radio buttons.
*/
ButtonGroup endSecTypes = new ButtonGroup();
JMenu end00Sec = new JMenu("00");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item00Unit = new JRadioButtonMenuItem(unitItems[x]);
item00Unit.addActionListener(this);
end00Sec.add(item00Unit);
endSecTypes.add(item00Unit);
item00Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeSecond = Integer.parseInt(event.getActionCommand());
System.out.println("endTimeSecond = " + endTimeSecond);
updateDisplay();
}
});
}
JMenu end10Sec = new JMenu("10");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item10Unit = new JRadioButtonMenuItem(unitItems[x]);
item10Unit.addActionListener(this);
end10Sec.add(item10Unit);
endSecTypes.add(item10Unit);
item10Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeSecond = Integer.parseInt(event.getActionCommand()) + 10;
updateDisplay();
}
});
}
JMenu end20Sec = new JMenu("20");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item20Unit = new JRadioButtonMenuItem(unitItems[x]);
item20Unit.addActionListener(this);
end20Sec.add(item20Unit);
endSecTypes.add(item20Unit);
item20Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeSecond = Integer.parseInt(event.getActionCommand()) + 20;
updateDisplay();
}
});
}
JMenu end30Sec = new JMenu("30");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item30Unit = new JRadioButtonMenuItem(unitItems[x]);
item30Unit.addActionListener(this);
end30Sec.add(item30Unit);
endSecTypes.add(item30Unit);
item30Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeSecond = Integer.parseInt(event.getActionCommand()) + 30;
updateDisplay();
}
});
}
JMenu end40Sec = new JMenu("40");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item40Unit = new JRadioButtonMenuItem(unitItems[x]);
item40Unit.addActionListener(this);
end40Sec.add(item40Unit);
endSecTypes.add(item40Unit);
item40Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeSecond = Integer.parseInt(event.getActionCommand()) + 40;
updateDisplay();
}
});
}
JMenu end50Sec = new JMenu("50");
for (int x=0; x < unitItems.length; x++) {
JMenuItem item50Unit = new JRadioButtonMenuItem(unitItems[x]);
item50Unit.addActionListener(this);
end50Sec.add(item50Unit);
endSecTypes.add(item50Unit);
item50Unit.addActionListener(new ActionListener() {
publicvoid actionPerformed(ActionEvent event) {
endTimeSecond = Integer.parseInt(event.getActionCommand()) + 50;
updateDisplay();
}
});
}
endSecSubMenu.add(end00Sec);
endSecSubMenu.add(end10Sec);
endSecSubMenu.add(end20Sec);
endSecSubMenu.add(end30Sec);
endSecSubMenu.add(end40Sec);
endSecSubMenu.add(end50Sec);
endMenu.add(endSecSubMenu);
/*
* Assemble the submenus of the Help menu
*/
JMenuItem subHelpItem = new JMenuItem("Instructions");
subHelpItem.addActionListener(this);
helpMenu.add(subHelpItem);
helpMenu.addSeparator();
subHelpItem = new JMenuItem("About");
subHelpItem.addActionListener(this);
helpMenu.add(subHelpItem);
/*
* Finally, add all the menus to the menubar
*/
menuBar.add(fileMenu);
menuBar.add(startMenu);
menuBar.add(endMenu);
menuBar.add(Box.createHorizontalGlue());
menuBar.add(helpMenu);
}
publicvoid actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem) (e.getSource());
if(e.getActionCommand().equals("New")){
System.out.println("Got New menu item");
}
elseif(e.getActionCommand().equals("Save")){
System.out.println("Got Save menu item");
}
elseif(e.getActionCommand().equals("Print")){
System.out.println("Got Print menu item");
}
elseif(e.getActionCommand().equals("Exit")){
System.out.println("Got EXIT menu item");
}
elseif(e.getActionCommand().equals("c")){
System.out.println("Got Start Time menu item");
}
elseif(e.getActionCommand().equals("Instructions")){
System.out.println("Got Instructions menu item");
Instructions inst = new Instructions();
inst.setVisible(true);
}
elseif(e.getActionCommand().equals("About")){
System.out.println("Got About menu item");
MessageDialog md = new MessageDialog(this,
"PTC",
"Data Logger Dump Program",
"By",
"William G. Bogdanowich");
}
}
/*
* Add Components to Layout Manager
*/
publicstaticvoid addComponent (Container container, Component
component, int gridx, int gridy,
int gridwidth, int gridheight, int fill,
int anchor, double weightx, double weighty)
throws AWTException {
LayoutManager lm = container.getLayout();
if (!(lm instanceof GridBagLayout)) {
thrownew AWTException ("Invalid layout should be GridBagLayout not: "
+ lm);
}
else {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx; //column's upper left corner
gbc.gridy = gridy; //row's upper left corner
gbc.gridwidth = gridwidth; //Specify X columns in component
//display area
gbc.gridheight = gridheight; //Specify X rows in component display area
gbc.fill = fill; //components area will be positioned
gbc.anchor = anchor; //component will be positioned in area
gbc.weightx = weightx; //how to distribute extra space in width
gbc.weighty = weighty; //how to distribute extra space in hight
((GridBagLayout)lm).setConstraints(component,gbc);
container.add(component, gbc);
}
}
/*
* Update Display Area for Selection of Time
*/
publicvoid updateDisplay() {
startDisplay.removeAll();
Label displayLabel = new Label("Start Time: " + startTimeMinute + ":" +
startTimeSecond + " " +
"End Time: " + endTimeMinute + ":" +
endTimeSecond);
startDisplay.add(displayLabel, "Right");
startDisplay.validate();
}
/*
* Listener interface must implement, not used
*/
publicvoid windowClosed(WindowEvent e) {}
publicvoid windowIconified(WindowEvent e) {}
publicvoid windowOpened(WindowEvent e) {}
publicvoid windowClosing(WindowEvent e) {
dispose(); // Release window resources
System.exit(0);
}
publicvoid windowDeiconified(WindowEvent e) {}
publicvoid windowActivated(WindowEvent e) {}
publicvoid windowDeactivated(WindowEvent e) {}
}
Yeah, this is the Z-order problem that bites just about everyone at some point, because it is easy to forget a leading J now and then. What happens is that the OS keeps track of the Z-order, a stacking order from back to front, of all the components to draw on the screen. The OS asks each component in back to front order to draw itself. Swing does the same thing when it draws all its own components.
When you mix AWT and Swing, what happens is Swing just asks for say a native Window on top of which it draws everything. But AWT asks for a native peer component for each AWT component.
So in your case the OS knew about the Window requested by Swing and the AWT Label. When it was time to draw, the OS first asked the Swing Window to draw itself, and it drew everything except the Label, including any dropped down menus. Then the OS asked the Label to draw itself, which overdraws anything behind it, including menus.