O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required