Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 7 Feb 1998 15:59:55 -0500 (EST)
From:      Kevin Street <street@iName.com>
To:        freebsd-java@FreeBSD.ORG
Subject:   Java bug with setMenuBar() 
Message-ID:  <199802072059.PAA21069@kstreet.interlog.com>

next in thread | raw e-mail | index | archive | help
I've found a bug (restriction? feature?) with setMenuBar.  I'd like to 
swap menu bars in my awt application in some circumstances.  When I
call setMenuBar the entire application window moves up & left on the
screen.  It works ok if I bracket the setMenuBar call with
setVisible(false) and show(), but the window flash looks ugly.

Any thoughts on why Java can't keep track of where it is when swapping 
the menu bar?

Here's a stripped down program that exhibits the problem.  It's only
about 130 lines so I'll just include it here - hopefully that's not
too excessive for those of you with expensive mail.

*** MenuBug.java ***

import java.awt.*;                          
import java.awt.event.*;

class MenuBug extends Frame
{
  private MenuBar mbar1;
  private MenuBar mbar2;
  private int currentbar;
         
  public static void main(String args[]) 
  {
    MenuBug window = new MenuBug("Menu Bug Demonstration Window");
    window.show();
  }
     
  MenuBug(String title) 
  {
    super(title);
    addWindowListener(new AppWindowListener ());
    setSize(500,300);
    mbar1 = buildMenuBar(1);
    mbar2 = buildMenuBar(2);
    setMenuBar(mbar1);
    currentbar = 1;
  }

  private void swapMenuBar() 
  {
//     setVisible(false);
    if (1 == currentbar) {
	setMenuBar(mbar2);
	currentbar = 2;
      } else {
	setMenuBar(mbar1);
	currentbar = 1;
      }
//     show();
  }

  private void showLocation() 
  {
    System.out.println("getInsets() returned " + getInsets());
    System.out.println("getLocation() returned " + getLocation());
    try 
      {
	System.out.println("getLocationOnScreen() returned " +
			   getLocationOnScreen());
      } 
    catch ( IllegalComponentStateException e )
      {
	  System.out.println("Not on screen");
      }
  }

  public void dispose() 
  {
    setVisible(false);
    super.dispose();
  }

  private class MIswapActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e) 
    {
      showLocation();
      swapMenuBar();
      showLocation();
    }
  }

  private class MIwhereActionListener implements ActionListener 
  {
    public void actionPerformed(ActionEvent e)
    {
      showLocation();
    }
  }
	
  private class MIexitActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e) 
    {
      dispose();
    }
  }

  private class AppWindowListener implements WindowListener
  {
    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) 
    {
      System.exit(0);
    }
    public void windowClosing(WindowEvent e) 
    {
      dispose();
    }
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
  }

  private MenuBar buildMenuBar(int which) 
  {
    Menu fileMenu;
    MenuBar mbar;
    MenuItem mi;
    
    mbar = new MenuBar();
    
    fileMenu = new Menu("File");
        
    fileMenu.add(mi = new MenuItem("Where"));
    mi.addActionListener(new MIwhereActionListener());

    fileMenu.add(mi = new MenuItem("Swap"));
    mi.addActionListener(new MIswapActionListener());
    
    if (1 == which) {
      fileMenu.addSeparator();
      fileMenu.add(mi = new MenuItem("Exit"));
      mi.addActionListener(new MIexitActionListener());
    }
    
    mbar.add(fileMenu);
    return mbar;
  }
}

-- 
Kevin Street
street@iName.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199802072059.PAA21069>