3

What is the difference between JMenuItem and JMenu?

JMenu m;
JMenuItem i;
Mureinik
  • 297,002
  • 52
  • 306
  • 350

2 Answers2

4

A JMenu is the menu itself, or as the documentation states:

An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar.

A JMenuItem is a single item in such a menu. Or, as the documentation describes it:

An implementation of an item in a menu. A menu item is essentially a button sitting in a list. When the user selects the "button", the action associated with the menu item is performed.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
-1

JMenu extends JMenuItem. The different is that JMenu is an item which can contain other JMenuItems. Think of it as being a SubMenu.

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • "...as being a SubMenu..."; actually it's "...has sub-menu(s)". – geowar Oct 11 '17 at 15:58
  • @geowar The JMenuBar is a menu which can recusivly contain JMenus. By that definition, all JMenus are submenus of the JMenuBar. Why the downvote? – Pétur Ingi Egilsson Oct 12 '17 at 04:04
  • Because JMenu being a sub-menu doesn't distinguish it from JMenuItems; they can both be sub-menus. OTOH JMenuItems can not have sub-menus where as JMenus can. That's the difference; can have sub-menus, not being a sub-menu. – geowar Oct 12 '17 at 20:29