Question

is there any way to add a JMenuItem to a JPanel so that I can create a button to show multiple options, like the latest news button in firefox, under the address bar?

I can only add JMenu and JMenuItems to a JMenuBar, JPopupMenu and other JMenus and JMenuitems

Is there any way to create in Java Swing a drop down Jbutton? (the ones with an down arrow in their left which shows more options to the user)

Thanks in advance

Was it helpful?

Solution

Finnally i implement the "show options button" with a simple Jutton, and a JPopupPane with the options to show:

In the ActionPerformedListener of the button i write this code:

popMenu.show(showOptionsButton,0,showOptionsButton.getHeight())

It works fine like a JMenu in a JMenuBar, but not exactly like a dropdown button, in which you can perform an action pressing the button or show more actions pressing the down arrow. I believe this can be done ussing two buttons "very close", then use the code above in the arrow button, but setting the "action button" as the component of the popup, so that the popup shows below of both buttons.

OTHER TIPS

A JMenuBar should only be added to a JFrame (setMenuBar()), not a JPanel.

A Swing drop down button is the way to go here.

See this article for a good discussion on various implementation propositions

alt text http://blogs.sun.com/geertjan/resource/dropdownbutton1-jl.png

The above drop-down button use the NetBeans UI Utilities API (platform7/modules/org-openide-awt.jar in any distribution of NetBeans IDE). You do not need to have NetBeans to run it: only this jar you have extracted from the NetBeans installation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top