Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 908
Telcontar Avatar asked Apr 08 '09 07:04

Telcontar


People also ask

How do you create a drop down menu in Java?

Create a Dropdown Menu Using JComboBox in Java Below, we first create the array of options to display in the dropdown list. JComboBox is a component and needs a frame to reside, so we create a JFrame object. Then, we create the JComboBox object and pass the options array as its argument in the constructor.

How do I add ActionListener to JMenu?

With an instance of JMenu you can't add an ActionListener, only with JMenuItem you can do it. We can add an ActionListener to JMenu , but it just doesn't have any effect. JMenu opens its child JMenu s and JMenuItem s when the mouse hovers over it.

What is JMenu in Java?

JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems . JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).

How do I create a swing menu?

In order to create menu items in Swing, you need to create new instances of JMenuItem and set different properties for them. You can create menu item with both text and icon. Creates a JMenuItem instance without icon or text. Creates a JMenuItem instance with a given icon.


2 Answers

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.

like image 73
Telcontar Avatar answered Oct 23 '22 17:10

Telcontar


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.

like image 42
VonC Avatar answered Oct 23 '22 18:10

VonC