Link to home
Start Free TrialLog in
Avatar of Shushan
Shushan

asked on

scrolling popup menu

Hi,
I'm using a "dynamic" JPopupMenu, (which mean that I add/remov MenuItem from the popup according to the user selection). As a result, if I have many MenuItem the popup menu gets cut so the user can’t see all the JMenuItem.
How can I present the popup with scrolling and set the max size of the popup?

Thanks,
Shushan
Avatar of Ovi
Ovi

I don't unbderstand exactly what you meen by "As a result, if I have many MenuItem the popup menu gets cut so the user can't see all the JMenuItem" but you can do the following :

Instead of adding/removing directly from the popup menu, you should keepthe content of it in a separate class, and recreate the popup with the desired content every time.

Supose you have a class PopupContentManager, like this :

public class PopupContentManager {
  private Vector popupItems;
  ......................
  public void addItem(JMenuItem item) {
    popupItems.addElement(item);
  }
  public void removeItem(JMenuItem item) {
    popupItems.removeElement(item);
  }

  public void showPopup(int x, int y) {
    JPopupMenu popup = new JPopupMenu();
    for(int i= 0; i<popupItems.size(); i++)
      popup.add((JMenuItem) popupItems.elementAt(i));
    popup.show(x, y)
  }
}
Avatar of Shushan

ASKER

My problem is with the display of the menu when I have many items in it (and not with the fact that it is a dynamic menu – I know how to add/remove menuItems to/from the popup). When I have many JmenuItem in the menu so that the height of the menu is bigger then the screen, the user can't see some of the JMenuItems.
What I need is a way to put the menu in a scroll pane.

thanks, Shushan
One solution is to put the menu like they do in a Java source editor for autocompletion, using JWindow. Instead of showing directly the popup you should show a JWindow containing a scrollPane with the desired popup. You must take care at this solution about the state of the JWindow, e.g. if was hidded by the application window you should destroy'it.
Another solution with graphical effect is to implement something like windows 2000 or 98 menus. You can create a special JMenuItem containing a down arrow/up arrow, which if pressed should hide/show the contents of the popup from the index 0 to his current index. To not destroy the JMenuItems you can create special menuItems which have the hidden state (or directly using their setVisible(true/false) method).

For the "arrow menu item" you can set the text to "\/" or "/\" to simulate the arrow, or write a custom JMenuItemUI class in which you actually draw a arrow. This class should have a refference tor the menu from which is part of.
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shushan

ASKER

thanks, i'll try it.
me too and good luck.
Avatar of Shushan

ASKER

thanks, i'll try it.
Avatar of Shushan

ASKER

thanks, i'll try it.