Java: How can I add a JMenu to a JPanel or create a drop down button?

Telcontar picture Telcontar · Apr 8, 2009 · Viewed 7.3k times · Source

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

Answer

Telcontar picture Telcontar · Apr 8, 2009

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.