EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

JDateTextField and DateListeners


Author: UnicMan
Author's WebSite: http://members.tripod.com/unicman

Use of the component:

JDateTextField accepts dates from the user. It will ignore other keys and generate an event if invalid date is entered. Also the date filters are kepts separate so that they can be used individually. The allowed formats as date are:

1. MM/DD/YYYY
2. MMM DD, YYYY
3. DD MMM, YYYY
4. MMMMM DD, YYYY
5. DD MMMMM, YYYY

Here the comma is optional and year can be of two digits.

Design of DateFilter


JDateTextField
     |
     +----[Uses]---- ShortDateAdapter ----\
     |                                    |
     +----[Uses]---- NormalDateAdapter ---+----[Extends]---- DateAdapter
     |                                    |
     \----[Uses]---- AnyDateAdapter ------/


As shown above JDateTextField uses three adapters for filtering corresponding date keys and validating them. JDateTextField also has supporting methods for setting and retrieving dates.


DateAdapter
     |
     +----[Implements]---- KeyListener
     |
     +----[Implements]---- InputMethodListener (only for JDK1.2 JTextField)
     |
     +----[Implements]---- FocusListener
     |
     \----[Uses]---- UMDateFormat


All the adapters for date extend DateAdapter. For compatibility with JDK1.1 and JDK1.2, it implements all of the three listeners. Otherwise, InputMethodListener needs to be handled only for JDK1.2 JFC components.

The benefit of having the adapters in separate classes is, they can be used individually for attaching only specific functionality (e.g. only key filtering or only validation) to the text fields. For example if you don't want validation when JDateTextField looses focus, you don't have that control using JDateTextField. But you can instantiate the desired adapter and attach it to JTextField.

UMDateFormat is a interface for defining your custom date parser and formatter. There are two methods 'parseDate' and 'formatDate'. You can implement UMDateFormat interface and attach it to any date adapter.

Using JDateTextField

JDateTextField accepts either short, normal or both of the date formats. The acceptable format can be specified when instatiating, or can be set using setFilter method.

1. SHORT - Accepts date in only MM/DD/YYYY format
2. NORMAL - Accepts date in only MMM DD, YYYY (and similar) format
3. ANY - Accepts date in any format

JDateTextField has four constructors -

1. JDateTextField() - Accepts any date format
2. JDateTextField( type ) - Accepts specified format (SHORT/NORMAL/ANY)
3. JDateTextField( type, Calendar ) - Same as above, but sets the date of the field initially.
4. JDateTextField( type, Date ) - Same as above, provided for backword compatibility.

If DateListener is attached to the JDateTextField, this field will generate event if invalid
date is entered by the user. So control can be gained for invalid date (possibly for showing error messages etc). Only one DateListener can be attached to one JDateTextField.

Code below shows -
1. how to use JDateTextField
2. how to gain control on invalid date input using DateListener
3. how to use the adapters individually


/**
 * @(#) TestPanel.java	1.0	99/05/23
 *
 * This code is designed for JDK1.2
 * Use tab spacing 4. Follow JavaDoc convention while coding.
 * Mail any suggestions or bugs to unicman@iname.com
 */
import	javax.swing.*;
//import	com.sun.java.swing.*;

import	um.filteredtext.*;
import	um.filteredtext.date.*;
import	um.event.*;
import	java.awt.*;
import	java.awt.event.*;
import	java.util.*;

/**
 * Test application for testing date field.
 *
 * @version	1.0	05/23/99
 * @author	UnicMan
 */
public class TestPanel
	extends		JPanel
	implements	WindowListener,
				DateListener
{
	public static final boolean	DEBUG = true;

	/**
	 * Default Constructor.
	 */
	public TestPanel()
	{	
		setLayout( new GridLayout(3,3,5,5) );

		////
		// Test components for testing short date filter ...
		////
		JDateTextField	dtfShort	= new JDateTextField(JDateTextField.SHORT);
		TextField	tfShort			= new TextField();
		ShortDateAdapter daShort	= new ShortDateAdapter( this );

		dtfShort.setDateListener( this );
		tfShort.addKeyListener( daShort );
		tfShort.addFocusListener( daShort );
		// Add all short date components ...
		add( new JLabel("Short date:") );
		add( dtfShort );
		add( tfShort );

		////
		// Test components for testing normal date filter ...
		////
		JDateTextField	dtfNormal	= new JDateTextField(JDateTextField.NORMAL);
		TextField	tfNormal		= new TextField();
		NormalDateAdapter daNormal	= new NormalDateAdapter();

		dtfNormal.setDateListener( this );
		tfNormal.addKeyListener( daNormal );
		tfNormal.addFocusListener( daNormal );
		// Add all short date components ...
		add( new JLabel("Normal date:") );
		add( dtfNormal );
		add( tfNormal );

		////
		// Test components for testing any date filter ...
		////
		JDateTextField	dtfAny	= new JDateTextField(JDateTextField.ANY);
		TextField	tfAny		= new TextField();
		AnyDateAdapter daAny	= new AnyDateAdapter();

		dtfAny.setDateListener( this );
		tfAny.addKeyListener( daAny );
		tfAny.addFocusListener( daAny );
		// Add all short date components ...
		add( new JLabel("Any date:") );
		add( dtfAny );
		add( tfAny );
	}

	/**
	 * Main method used by Interpreter.
	 */
	public static void main( String	aszArgs[] )
	{
		if( DEBUG )	System.out.println( "TestPanel started." );

		JFrame		jFrame = new JFrame();
		TestPanel	tPanel = new TestPanel();
		jFrame.setContentPane( tPanel );

		Dimension	dScreen		= Toolkit.getDefaultToolkit().getScreenSize();
		Dimension	dContent 	= new Dimension(300,100);
		jFrame.setLocation(
			(dScreen.width-dContent.width)/2,
			(dScreen.height-dContent.height)/2
		);
		jFrame.setSize( dContent );
		jFrame.addWindowListener( tPanel );
		jFrame.show();
	}

	public void windowActivated( WindowEvent weEvent )	{}
	public void windowDeactivated( WindowEvent weEvent ){}
	public void windowIconified( WindowEvent weEvent )	{}
	public void windowDeiconified( WindowEvent weEvent ){}
	public void windowOpened( WindowEvent weEvent )		{}
	public void windowClosed( WindowEvent weEvent )		{}

	public void windowClosing( WindowEvent weEvent )
	{
		if( DEBUG )	System.out.println( "TestPanel Exited." );
		System.exit(0);
	}

	public void gotInvalidDate( DateEvent deEvt )
	{
		System.out.println( "Got dirty date: " + deEvt.getInvalidText() );
	}
}

Download JDK1.2 demo

Download DateFilter source code

Posted On: 25-Jul-1999

internet.commerce