// -----------------------MenuExample.java-------------------------- import com.sun.java.swing.*; public class MenuExample extends JFrame { public MenuExample() { JMenuBar mb = new JMenuBar(); newMenu(mb); newMenu(mb); newMenu(mb); setJMenuBar(mb); } public void newMenu(JMenuBar mb) { JMenu m = (JMenu)mb.add(new JMenuPlus("File")); m.add("Menu item"); m.add("Menu item"); m.add("Menu item"); JMenu m1 = (JMenu)m.add(new JMenuPlus("menu")); m1.add("Submenu item"); m1.add("Submenu item"); } public static void main(String args[]) { MenuExample f = new MenuExample(); f.pack(); f.show(); } } // -------------------------------------------------------------- // -------------------------JMenuPlus.java----------------------- /* * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import com.sun.java.swing.*; import java.awt.*; /** * A subclass of JMenu which adds a simple heuristic for ensuring * that the popup menu gets placed onscreen. */ public class JMenuPlus extends JMenu { public JMenuPlus(String label) { super(label); } /** * Override the implementation in JMenu in order to be more * intelligent about determining the placement of the popup menu. * * @param b a boolean value -- true to make the menu visible, * false to hide it */ public void setPopupMenuVisible(boolean b) { boolean isVisible = isPopupMenuVisible(); if (b != isVisible) { // Set location of popupMenu (pulldown or pullright) // Perhaps this should be dictated by L&F if ((b==true) && isShowing()) { Point p = getPopupMenuOrigin(); getPopupMenu().show(this, p.x, p.y); } else { getPopupMenu().setVisible(false); } } } /** * Override the implementation in JMenu in order to be more * intelligent about determining the placement of the popup menu. * * @returns a Point in the coordinate space of the menu instance * which should be used as the origin of the JMenu's popup menu. */ protected Point getPopupMenuOrigin() { int x = 0; int y = 0; JPopupMenu pm = getPopupMenu(); // Figure out the sizes needed to caclulate the menu position Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize(); Dimension s = getSize(); Dimension pmSize = pm.getSize(); // For the first time the menu is popped up, // the size has not yet been initiated if (pmSize.width==0) { pmSize = pm.getPreferredSize(); } Point position = getLocationOnScreen(); Container parent = getParent(); if (parent instanceof JPopupMenu) { // We are a submenu (pull-right) // First determine x: if (position.x+s.width + pmSize.width < screenSize.width) { x = s.width; // Prefer placement to the right } else { x = 0-pmSize.width; // Otherwise place to the left } // Then the y: if (position.y+pmSize.height < screenSize.height) { y = 0; // Prefer dropping down } else { y = s.height-pmSize.height; // Otherwise drop 'up' } } else { // We are a toplevel menu (pull-down) // First determine the x: if (position.x+pmSize.width < screenSize.width) { x = 0; // Prefer extending to right } else { x = s.width-pmSize.width; // Otherwise extend to left } // Then the y: if (position.y+s.height+pmSize.height < screenSize.height) { y = s.height; // Prefer dropping down } else { y = 0-pmSize.height; // Otherwise drop 'up' } } return new Point(x,y); } }