next up previous
Next: Option dialogs Up: Simple Dialogs Previous: Simple Dialogs

A simple dialog

We will create an application frame containing a single button which, when clicked, creates a new instance of a JDialog box.

The program SimpleDialog creates a new class, TestDialog, derived from the Swing JDialog class

The code listing for this SimpleDialog,java is:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class SimpleDialog
		extends 	JFrame
		implements	ActionListener
 {
	// Instance attributes used in this example
	private	JPanel		topPanel;
	private	JButton	buttonDialog;


	// Constructor of main frame
	public SimpleDialog()
	{
		// Set the frame characteristics
		setTitle( "Dialog Test Frame" );
		setSize( 310, 130 );
		setBackground( Color.gray );

		// Create a panel to hold all other components
		topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

		// Create a button to get things started
		buttonDialog = new JButton( "Open Dialog" );
		topPanel.add( buttonDialog, BorderLayout.CENTER );

		// add an action listener to listenr for button clicks
		buttonDialog.addActionListener( this );
	}

	// ActionListener handler to listener for button clicks
	// within this application fram
	public void actionPerformed( ActionEvent event )
	{
		// Display a message on the console
		System.out.println( event );

		// Create an instance of the test dialog
		TestDialog testDialog = new TestDialog( this );
		testDialog.show();
	}

	// Main entry point for this example
	public static void main( String args[] )
	{
		// Create an instance of the test application
		SimpleDialog mainFrame	= new SimpleDialog();
		mainFrame.setVisible( true );
	}
}

and for the TestDialog.java class:

// Imports
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TestDialog
		extends 	JDialog
 {
	// Instance attributes used in this dialog
	private	JFrame		parentFrame;
	private	JScrollPane	scrollPane1;


	// Dialog constructor
	public TestDialog( JFrame parentFrame )
	{
		// Make sure we call the parent
		super( parentFrame );

		// Save the owner frame in case we need it later
		this.parentFrame = parentFrame;

		// Set the characteristics for this dialog instance
		setTitle( "Test Dialog" );
		setSize( 200, 200 );
		setDefaultCloseOperation( DISPOSE_ON_CLOSE );


		// Create a panel for the components
		JPanel topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

		// Populate the panel with something the user
		// can play with
		CreateTopPane( topPanel );
	}


	private void CreateTopPane( JPanel topPanel )
	{
		// Create a text area
		JTextArea area = new JTextArea();

		// Load a file into the text area, catching any exceptions
		try {
			FileReader fileStream = new FileReader( "SimpleDialog.java" );
			area.read( fileStream, "SimpleDialog.java" );
		}
		catch( FileNotFoundException e )
	   	{
	   	    System.out.println( "File not found" );
	   	}
	   	catch( IOException e )
	   	{
	   	    System.out.println( "IOException occurred" );
	   	}

		// Create the scrolling pane for the text area
		scrollPane1 = new JScrollPane();
		scrollPane1.getViewport().add( area );
		topPanel.add( scrollPane1, BorderLayout.CENTER );
	}
}



Dave Marshall
4/14/1999