|
|
Title |
Simple modal dialog
|
Summary |
How to create a simple modal dialog box. Simular to the about box that comes with many applications. |
Contributor |
John McTainsh
|
Published |
27-Oct-2000 |
Last updated |
27-Oct-2000 |
|
|
Description.
Displaying a modal dialog can be handy for such things as
about box and warning message boxes. This SimpleDialog
class displays a Dialog box with a text area in it.
Simple Dialog class
The class loads and creates an action listener to destroy
itself when the the close is selected. It also add a text
area to display the data in the dialog.
/////////////////////////////////////////////////////////////////////////////
//Modal Dialog box used to display a modal about box
class SimpleDialog extends Dialog
{
//Attributes
private TextArea m_textArea;
//Constructor
public SimpleDialog( Frame fParent, String sTitle )
{
super( fParent, sTitle, true );
}
//Layout and show the dialog
public void show( String sText )
{
//Add anonymous class handler to Close dialog with close button
addWindowListener (
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
if( e.getSource() instanceof SimpleDialog )
{
SimpleDialog sd = (SimpleDialog)( e.getSource() );
sd.dispose();
}
else
{
System.out.println( "***ERROR event recieved for wrong window " + e );
}
}
} );
m_textArea = new TextArea( sText );
setSize( 200, 100 );
add( m_textArea );
show(); //This is a blocking call
}
}
This is how to use the call the dialog. We create a simple frame with two
buttons, one to activate the Dialog and one to quite the application.
import java.awt.*;
import java.awt.event.*;
/////////////////////////////////////////////////////////////////////////////
//Typical Class that catchs a button event and display
//a modal dialog box.
public class DialogTest extends Frame implements ActionListener
{
//Attributes
private Button m_btnDialog;
private Button m_btnQuit;
//Main application starts here
public static void main (String[] args)
{
DialogTest frame = new DialogTest();
frame.setTitle( "Testing Dialog!" );
frame.SetupMain();
}
//Setup initial
public void SetupMain()
{
setLayout( new FlowLayout() );
m_btnDialog = new Button( "About" );
m_btnDialog.addActionListener( this );
m_btnQuit = new Button( "Quit" );
m_btnQuit.addActionListener( this );
add( m_btnDialog );
add( m_btnQuit );
pack();
setVisible( true );
}
//Event handler (For button press only)
public void actionPerformed(ActionEvent e)
{
//Display Dialog if Button is pressed
if( e.getSource() == m_btnDialog )
{
SimpleDialog simdlg = new SimpleDialog( this, "About box" );
simdlg.show( "Application by John McTainsh\n" +
"Written 26 October 2000." );
}
//Quite if Button is pressed
if( e.getSource() == m_btnQuit )
{
System.exit( 0 );
}
}
}
NOTE: When running the Microsoft virtual machine JView.exe
you may be able to click on the "About" button several times before
the first instance of the About box appears so several About boxes
may appear on top of each other. This does not happen on the
SUN virtual machine. To overcome this prevent the Dialog
displaying if it is already visible. ie
...
//Event handler (For button press only)
SimpleDialog m_SimDlg = null;
public void actionPerformed(ActionEvent e)
{
//Display Dialog if Button is pressed
if( e.getSource() == m_btnDialog )
{
if( m_SimDlg != null )
return;
m_SimDlg = new SimpleDialog( this, "About box" );
m_SimDlg.show( "Application by John McTainsh\n" +
"Written 26 October 2000." );
m_SimDlg = null;
}
...
|