Skip to main content

JPopupMenu in a ScrollPane

6 replies [Last post]
Anonymous

Hi All,

At present there is a JButton and when I click the JButton, JPopupMenu will be dispalyed below the JButton (similar to the popup in combobox kind of stuff.....)I am adding many menu items in the JPopupMenu. Upto this everything is fine.

But, I want to put all the menu items to a scrollable JPopupMeny.Like if there are many menu items then I have to put those items in the scollable popup so that I can navigate using scrollbar when there are many meni items.

I think I can add a JScrollPane to the JPopupMenu and put a JList inside...But I havent implemented that and I wish to know whether I can finish this with JPopupMenu and MenuItems as I didn't like the solution I said :-).

Thanks in advance,
Jana

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zander
Offline
Joined: 2003-06-13

Perhaps the combobox popup is something you can use?

See javax.swing.plaf.basic.BasicComboPopup

Good luck!

Anonymous

Hi,

Thanks for your reply. I already had a look into that. The problem is I don't want to go to that solu with JList becos I am already having a JPopupMenu and MeniItems in my component. If I try to implement that ComboBox behavior, then I have to change lot of my codes. I want to do minimum changes. So, I want to know whether I can use JPopupMenus, Menus, MenuItem and ScrollPane.....barely minimal changes but trying to achieve the functionality.

Thanks,
Jana

zander
Offline
Joined: 2003-06-13

The class I mentioned extends JPopupMenu, so its a drop-in replacement...

I'm not really sure what you are asking here.. Are you unsure that the scrollpane/jlist proposal works?

These are problems you hava to face;
- when selecting an entry, you have to close the popup manually.
- you have to know a bit about maximumSizes etc since you don't want the popup to get too big.
- don't forget to set the list to single selection!
- when loosing focus the popup should also close, it does not do that automatically.
- do a pack() before showing it ;)

All in all; I feel using the above class is definitely worth it. Reuse almost always beats coding it again :)

Anonymous

Hi,

Actually I am having a button now and whenever somebody clicks on the button JPopupMenu shows. In that JPopupMenu, I add some MenuItems directly to the JPopupMenu. This is the current behavior. And moreover I have some ActionListeners attached to all those menu items.

Now the requirement is if there are too many menuitems then JPopupMenu should scroll with vertical scrollbar attached to it.
What I am asking is whether is it is possible to put those menu items to menus and add that menu to the JScrollPane. And then shall I add that scrollpane to the JPopupMenu (surely I can do this). By doing this, I have to do very less changes to my code.

I cud understand what you are saying like putting the JList in the ScrollPane and adding it to the JPopupMenu. I do understand that it will be a good solution. But at this point in my code, I slightly hesitate to do those modifications. I my above said idea workd(prev para), then I have to do little changes and I will be happy tooo.

Finally, inshort....I just want to retain the MenuItems that is alreadypresent in the JPopupMenu...I just want some component in which I can put all those and add that to the ScrollPane and then to JPopupMenu..

Thanks,
Jana

zander
Offline
Joined: 2003-06-13

> Now the requirement is if there are too many
> menuitems then JPopupMenu should scroll with vertical
> scrollbar attached to it.
> What I am asking is whether is it is possible to put
> those menu items to menus and add that menu to the
> JScrollPane. And then shall I add that scrollpane to
> the JPopupMenu (surely I can do this).

It seems easy; but I can't get it working.

[code]
JPopupMenu p = new JPopupMenu();

JPanel content = new JPanel(new NaturalLayout(1,100));
for(int i=0; i < 100; i++)
content.add(new JMenuItem("item " + i));

JScrollPane scroller = new JScrollPane() {
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = Math.min(d.width, 500);
d.height = Math.min(d.height, 500);
return d;
}
};
scroller.getViewport().add(content, null);

p.add(scroller);
p.pack();
p.show(pushButton2.getParent(), pushButton2.getX(), pushButton2.getY() + pushButton2.getHeight());

[/code]

As soon as I move the pointer over the list; it dissapears.

Can't you use submenu's?

I'm afraid you will have to experiment a little to get this right, I always start with a new class which has a main() to experiment. That is the best way to find out how to finish this correctly. As you know; its better to have a bad implementation of a good design then to have a good implementation of a bad design.
In other words; please go for correct design and not an easy solution.

> Finally, inshort....I just want to retain the
> MenuItems that is alreadypresent in the
> JPopupMenu...I just want some component in which I
> can put all those and add that to the ScrollPane and
> then to JPopupMenu..

Again I point to the combobox popup, it provides exactly what you need.

Anonymous

Thanks, I will try and let you know.

Jana