I have Multiple Items in Menu which goes off screen. How can I limit the height of the menu and see all the menu Items?

Vikram Lele

Instead of reducing the height of the individual menu option, better approach will be to make the menu smarter.

Look at any of the windows applications, when the menu is too close to the bottom edge of the screen, menu opens upwards, rather than downwards.

You can derive a class from JMenu, and add menu event listener :

public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
And then change the menu position so that instead of opening in the stadard direction, it opens in the opposite direction.

void adjustPopupPosition(JPopupMenu aMenu) {
   JMenu envokerMenu = (JMenu)aMenu.getInvoker();

   if(envokerMenu.isTopLevelMenu()) {
      // Top level menus can open up or down
      int nMenuTop = envokerMenu.getLocationOnScreen().y;
      int nMenuHeight = envokerMenu.getSize().height;
      int nPopupHeight = aMenu.getSize().height;
      int nScreenHeight = aMenu.getToolkit().getScreenSize().height;

      if(nScreenHeight < (nMenuTop + nMenuHeight + nPopupHeight)) {
         aMenu.setLocation(envokerMenu.getLocationOnScreen().x, nMenuTop - nPopupHeight);
   else {
         // non top level menu can open on left or right
      int nMenuLeft = envokerMenu.getLocationOnScreen().x;
      int nMenuWidth = envokerMenu.getSize().width;
      int nPopupWidth = aMenu.getSize().width;
      int nScreenWidth = aMenu.getToolkit().getScreenSize().width;

      if(nScreenWidth < (nMenuLeft + nMenuWidth + nPopupWidth)) {
         aMenu.setLocation(nMenuLeft - nPopupWidth, envokerMenu.getLocationOnScreen().y);
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact