United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-8007006 : [macosx] Closing subwindow loses main window menus.

Details
Type:
Bug
Submit Date:
2013-01-28
Status:
Resolved
Updated Date:
2014-04-28
Project Name:
JDK
Resolved Date:
2013-01-31
Component:
client-libs
OS:
os_x
Sub-Component:
javax.swing
CPU:
Priority:
P4
Resolution:
Fixed
Affected Versions:
7,8
Fixed Versions:

Related Reports
Backport:
Backport:
Duplicate:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin Mac-User.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:26:45 PDT 2012; root:xnu-1699.32.7~1/RELEASE_I386 i386

Mac OS X 10.7.5

A DESCRIPTION OF THE PROBLEM :
An OS X application using the...
apple.laf.useScreenMenuBar=true
property loses it's main JFrame menus after a different JFrame is dismissed.
Both do this...
   		menuBar = new JMenuBar();
 		setJMenuBar(menuBar);

If the other JFrame does not do this the problem didn't appear to occur.
The other JFrame is dismissed with a cancel button which does a
setVisible(false);
dispose()


REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test case with something like...
java -Dapple.laf.useScreenMenuBar=true MainMenu
  From the screen menubar select New Sub from the SubWindow menu.
The subwindow should appear in the upper left hand corner.
Select the cancel button.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The main window activates and regains focus with it's menu in place.
ACTUAL -
The main window activates and regains focus but it's SubWindow menu is missing.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MainMenu extends JApplet implements ActionListener {
	static JApplet applet = null;
	JFrame f = null;
 	private static javax.swing.JMenuBar menuBar;
	
	public static void main(String[] args) {
	 	applet = new MainMenu();
	 	((MainMenu)applet).prepare();
		((MainMenu)applet).frameIt();
	}
	
	public void frameIt() {
		f = new JFrameMenu("Menu Main");
		f.getContentPane().add(applet);
		if (System.getProperty("os.name").equals("Mac OS X"))
			f.setJMenuBar(menuBar);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
		f.setBackground(Color.white);
        ((JComponent) f.getContentPane()).setBorder(new EmptyBorder(9,17,17,17));
		f.pack();
		MainMenu.positionWindow(f);
		f.setVisible(true);
		f.requestFocus();
	}
	
	class JFrameMenu extends JFrame {
		static final long serialVersionUID = 5348828834011481281L;
		public JFrameMenu(String title) { super(title); }
	}
		
	public void prepare() {
		getContentPane().setBackground(Color.white);
		menuBar = new JMenuBar();
		JMenu subM = new JMenu("SubWindow");
		JMenuItem newSub = new JMenuItem(new AcceleratedAction("New Sub",KeyStroke.getKeyStroke(KeyEvent.VK_I,Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),this));
		subM.add(newSub);
		menuBar.add(subM);

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        
 		getContentPane().setLayout(gbl);
		c.insets = insets(0,0,3,0);
  		c.anchor = GridBagConstraints.CENTER;
  		c.gridy = 0;
  		c.gridwidth = 8;
  		JLabel l = new JLabel("Main Window");
  		Font f = l.getFont();
  		f = f.deriveFont(18f);
  		l.setFont(f);
  		l.setForeground(Color.blue);
  		getContentPane().add(l,c);
	}

	public void actionPerformed(final ActionEvent evt) {
		final String cmd = evt.getActionCommand();
	}
	
	public Insets insets(int t, int l, int b, int r)
	{
		return new Insets(t,l,b,r);
	}
		
	private static void positionWindow(Window w)
	{
		Dimension sSize = w.getToolkit().getScreenSize();	// Position the window
  	 	int sHeight = sSize.height;
  	 	int sWidth = sSize.width;
  	 	Dimension aSize = w.getSize();
  	 	int aHeight = aSize.height;
  	 	int aWidth = aSize.width;
 	 	w.setLocation((sWidth-aWidth)/2,(sHeight-aHeight)/2);
	}
}

class AcceleratedAction extends AbstractAction implements ActionListener {
	 static final long serialVersionUID = 6812537624479959853L;
	 
	 ActionListener l = null;
	 
	 AcceleratedAction(String title,KeyStroke ks,ActionListener l) {
		 super(title);
		 putValue(ACCELERATOR_KEY,ks);
		 this.l = l;
	 }
     	
     public void actionPerformed(ActionEvent evt) {
    	 l.actionPerformed(evt);
    	 SubWindow sw = new SubWindow();
    	 sw.pack();
    	 sw.setVisible(true);
     }
}

class SubWindow extends JFrame implements ActionListener {

 	private javax.swing.JMenuBar menuBar;

	SubWindow() {
		super("SubWindow");
		subPrepare();
	}

	public void subPrepare() {
		getContentPane().setBackground(Color.white);
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        
 		getContentPane().setLayout(gbl);
		c.insets = new Insets(0,0,3,0);
  		c.anchor = GridBagConstraints.CENTER;
  		c.gridy = 0;
  		c.gridwidth = 8;
  		JLabel l = new JLabel("Sub Window");
  		Font f = l.getFont();
  		f = f.deriveFont(18f);
  		l.setFont(f);
  		l.setForeground(Color.blue);
  		getContentPane().add(l,c);
		c.gridx = 0;
 		c.gridy = 2;
		c.gridwidth = 4;
		c.gridheight = 1;
		JPanel buttons = new JPanel();
		buttons.setBackground(Color.white);
		c.anchor = GridBagConstraints.WEST;
		getContentPane().add(buttons,c);
 		JButton cancel = new JButton("Cancel");
 		cancel.setBackground(Color.white);
		buttons.add(cancel);
 		cancel.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				setVisible(false);
 				dispose();
 			}
 		});
   		menuBar = new JMenuBar();
 		setJMenuBar(menuBar);
		JMenu subM = new JMenu("SubWindow");
		JMenuItem newSub = new JMenuItem(new AcceleratedAction("Say Sub",KeyStroke.getKeyStroke(KeyEvent.VK_I,Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),this));
		subM.add(newSub);
		menuBar.add(subM);
	}
	
	public void actionPerformed(ActionEvent evt) {
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Giving another application focus and then reselecting the test application restores the menu.

I believe in coming up with the test case the removing the
		menuBar = new JMenuBar();
 		setJMenuBar(menuBar);
also seemed to work. I had to add this to cause the faiure.
                                    

Comments
URL:   http://hg.openjdk.java.net/jdk8/awt/jdk/rev/fd61fcc1a5a9
User:  leonidr
Date:  2013-01-31 14:26:45 +0000

                                     
2013-01-31
URL:   http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/fd61fcc1a5a9
User:  lana
Date:  2013-02-12 18:23:17 +0000

                                     
2013-02-12
A regression test test/java/awt/MenuBar/8007006/bug8007006.java has been added.
                                     
2013-11-12



Hardware and Software, Engineered to Work Together