Solved

scrolling popup menu

Posted on 2002-04-06
9
810 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:Shushan
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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)
  }
}
0
 

Author Comment

by:Shushan
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Accepted Solution

by:
Ovi earned 80 total points
Comment Utility
Sorry, not JMenuItemUI but MenuItemUI.
0
 

Author Comment

by:Shushan
Comment Utility
thanks, i'll try it.
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
me too and good luck.
0
 

Author Comment

by:Shushan
Comment Utility
thanks, i'll try it.
0
 

Author Comment

by:Shushan
Comment Utility
thanks, i'll try it.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now