Menus

In Java there are two types of menus: menubar and popup. The following section shows how to add a menubar to a frame.

JMenu Bar

Import the JMenuBar class from java.swing.

>>> from javax.swing import JJMenuBar

Create an instance of it.

>>> bar = JJMenuBar()

Import and create an instance of JFrame.

>>> from javax.swing import JFrame
>>> frame = JFrame()

Set the JMenuBar property of the frame to the JMenuBar instance.

>>> frame.JJMenuBar = bar
>>> frame.visible=1

Now that we have a menubar added in the frame, we need to add a menu to it. Create a menu (JMenu), and add it to the menubar.

>>> from javax.swing import JMenu
>>> fruitMenu = JMenu("Fruit")
>>> bar.add(fruitMenu)
>>> frame.pack()

Add menu items to the menu.

 >>> from javax.swing ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.