Chapter 14. Menus and Toolbars

This chapter discusses Swing menus and toolbars. Menus are by far the larger and more extensible of the two, so they encompass most of the chapter. Swing offers the programmer a fully reimplemented menu system from AWT 1.1. This re-engineering allows you to use the Swing menus in a variety of eye-catching ways, and provides much more freedom to lay out menu components.

Toolbars are a new addition to Swing. Toolbars allow the programmer to group buttons, combo boxes, and other components together in repositionable panels; these tools can assist the user in performing many common tasks. You can add any component to a Swing toolbar, even non-Swing components. In addition, Swing allows the toolbar to be dragged from the frame and positioned inside a child window for convenience.

Introducing Swing Menus

If you’ve worked with menus before, you know that one of the most frustrating issues with AWT is that almost all of its menu components extend the abstract java.awt.MenuComponent class. This is an awkward design, as the remaining AWT components extend the more versatile java.awt.Component class. Consequently, menu components are divorced from regular components in AWT, since subclasses of MenuComponent can perform only a fraction of the others’ functionality. In addition, AWT programmers are locked into placing a single menubar at the top of each Frame and cannot position menu components in containers using layout managers.

With Swing, this hierarchy has been ...

Get Java Swing 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.