Java Reference
In-Depth Information
f . setVisible( true );
class AboutFrame extends JFrame
public AboutFrame ()
setSize (300 , 300) ;
JMenuBar menuBar = new JMenuBar ( ) ;
setJMenuBar(menuBar) ;
JMenu helpMenu = new JMenu( "Help" );
menuBar . add(helpMenu) ;
JMenuItem aboutMI = new JMenuItem( "About" );
helpMenu . add(aboutMI) ;
aboutMI. addActionListener( new ActionListener ()
public void actionPerformed(ActionEvent e)
AboutDialog dialog = new AboutDialog (AboutFrame . this );
dialog . setSize (100 , 100) ;
dialog . setVisible( true );
} );
class AboutDialog extends JDialog
public AboutDialog(JFrame owner)
super (owner, "About" , true );
JLabel label = new JLabel( "It's all about me!" );
add( label );
Examine the constructor of the AboutDialog class. The first line simply creates a
JDialog with the title About . A reference to the parent window is needed in order
to create the dialog. The third parameter simply makes the panel modal. Next, exam-
ine the actionPerformed method that is called when the menu item is selected. The
AboutFrame.this reference refers to the current object of the outer class. We need this
syntax because the actionPerformed method is inside a local anonymous class and the
this reference will return a reference to the event listener. Lastly, note that an object of
type JDialog is similar to an object of type JFrame . Methods, such as setVisible and
setSize are still applicable. The first method sets the size of the dialog box, while the
second method makes the dialog box visible.
Run the application. You will see that the about dialog box is really all about itself.
Nothing can be done until it is closed. Alternatively, change the third parameter of the
constructor of the JDialog class to false . Now a new dialog can be open while the current
one exists, which is probably not the desired behavior.
12.12 Working with Password Fields
The final topic in this chapter will be how to create password fields. This can be done
by instantiating an object from the JPasswordField class. Unlike a text field, a password
Search WWH ::

Custom Search