Java Reference
In-Depth Information
Chapter 20
Extending the GUI
WHAT YOU WILL LEARN IN THIS CHAPTER:
• How to create a status bar
• How to create a dialog
• What a modal dialog is and how it differs from a modeless dialog
• How to create a message box dialog
• How you can use components in a dialog to receive input
• What a pop-up menu is
• How you can apply and use transformations to the user coordinate system when drawing on a com-
ponent
• What context menus are and how you can implement them
In this chapter you investigate how you can improve the graphical user interface (GUI) for Sketcher. After
adding a status bar, you create dialogs and explore how you can use them to communicate with the user and
manage input. You look at context menus, which are pop-up menus that vary depending on the context in
which they are displayed. You use context menus to enhance the functionality of the Sketcher application. All
of this gives you a lot more practice in implementing event listeners, and much more besides.
CREATING A STATUS BAR
One limitation of the Sketcher program as it stands is that you have no direct feedback on what the current
element type is and what its color is. As a gentle start to this chapter, let's fix that now. A window status bar at
the bottom of an application window is a common and very convenient way of displaying the status of various
application parameters, each in its own pane.
There is no Swing class that defines a status bar, so you have to make up your own StatusBar class.
Ideally you would design a class for a general-purpose status bar and customize it for Sketcher, but as space
in the topics is limited I'm taking the simple approach of showing how you can design a class that is specific
to Sketcher. The JPanel class is a good basis for the StatusBar class because it represents a panel, and you
can add objects to it that represent status bar panes. You use the JLabel class as a base for defining status bar
panes and add sunken borders to them to give them a distinctive appearance.
A Status Bar Class for Sketcher
Search WWH ::




Custom Search