Java Reference
In-Depth Information
Directory "Sketcher 2 displaying an About dialog"
The constructor first calls the base JDialog class constructor to create a modal dialog with the title bar
given by the title argument. It then defines the position of the dialog relative to the position of the frame.
NOTE Whenyoucreateaninstanceofthe AboutDialog classintheSketcher programalittle
later in this chapter, you specify the SketcherFrame object as the parent for the dialog. The
parent relationship between the application window and the dialog implies a lifetime depend-
ency. When the SketcherFrame object is destroyed, the AboutDialog object is, too, because
it is a child of the SketcherFrame object. This doesn't just apply to JDialog objects — any
Window object can have another Window object as a parent.
By default the AboutDialog window is positioned relative to the top-left corner of the screen. To position
the dialog in a more sensible position relative to the application window, you set the coordinates of the top-
left corner of the dialog as one quarter of the distance across the width of the application window, and one
quarter of the distance down from the top-left corner of the application window.
You add the components you want to display in a dialog to the content pane for the JDialog object. The
content pane has a BorderLayout manager by default, just like the content pane for the application window,
and this is quite convenient for the AboutDialog layout. The dialog contains two JPanel objects that are
created in the constructor, one to hold a JLabel object for the message that is passed as an argument to the
constructor and the other to hold the OK button that closes the dialog. The messagePane object is added so
that it fills the center of the dialog window. The buttonPane position is specified as BorderLayout.SOUTH ,
so it is at the bottom of the dialog window. Both JPanel objects have a FlowLayout manager by default.
You want the AboutDialog object to be the listener for the OK button so you pass the this variable as
the argument to the addActionListener() method call for the button.
The pack() method is inherited from the Window class. This method packs the components in the win-
dow, setting the window to an optimal size for the components it contains before laying out the components.
Note that if you don't call pack() here, the size for your dialog is not set and you aren't able to see it.
The actionPerformed() method is called when the OK button is selected. This just disposes of the dia-
log by calling the dispose() method for the AboutDialog object so the dialog window disappears from the
screen and the resources it was using are released.
Creating an About Menu Item
To add a Help menu with an About item to the Sketcher application, you need to insert some code into the
SketcherFrame class constructor. You can make the SketcherFrame object the listener for the About menu
item by making it implement the ActionListener interface and adding the actionPerformed() method
implementation. Let's see it working.
TRY IT OUT: Displaying an About Dialog
Search WWH ::




Custom Search