osx, java7 screen menu empty after frame.dispose

Roger Tönz roger.toenz at abacus.ch
Wed Apr 17 06:40:02 PDT 2013


hi

i've got a little Problem with the ScreenMenu

I've several Windows with their own JMenuBar.

When one of the Frames is closed with the DISPOSE_ON_CLOSE behavior, the apple-screenmenu is empty.

Below a little Example.

Roger

Mac OS X 10.8, Java 1.7u17




import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class SwingFrameTest extends JFrame {

  static int counter = 0;


  public static void main(String[] args) {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    
    SwingFrameTest frameTest = new SwingFrameTest();
    frameTest.setTitle( "MainWindow");
    JTextArea text = new JTextArea();
    text.setText("Open two Window with the Menu=>Windows=>new\n and then close one. \nNow the Menu is empty");
    frameTest.getContentPane().add(text);
    initMenuBar(frameTest);
    frameTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frameTest.setVisible(true);
  }

  static void initMenuBar(final JFrame frame){
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    bar.add(new JMenu(frame.getTitle()));
    
    if(frame instanceof SwingFrameTest){
      JMenu datei = new JMenu("Windows");
      bar.add(datei);
      JMenuItem item = new JMenuItem("new");
      datei.add(item);
      item.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
          JFrame newFrame = new JFrame("Frame"+counter++);
          initMenuBar(newFrame);
          newFrame.setVisible(true);
        }});
    }

    frame.setJMenuBar(bar);
  }


}



More information about the macosx-port-dev mailing list