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!
-----

-


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

Use of the component:

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

1. HH:MM:SS.SSS
2. HH:MM:SS.SSSxx (AM/PM)

Here minutes and seconds are optional. If BASE12 style is set, AM/PM must be specified.

Design of JTimeTextField


JTimeTextField
     |
     +----[Uses]---- Base24TimeAdapter ---\
     |                                    |
     +----[Uses]---- Base12TimeAdapter ---+----[Extends]---- TimeAdapter
     |                                    |
     \----[Uses]---- AnyTimeAdapter ------/


As shown above JTimeTextField uses three adapters for filtering corresponding time keys and validating them. JTimeTextField also has supporting methods for setting and retrieving times.


TimeAdapter
     |
     +----[Implements]---- KeyListener
     |
     +----[Implements]---- InputMethodListener (only for JDK1.2 TextField)
     |
     \----[Implements]---- FocusListener



All the adapters for time extend TimeAdapter. 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 JTimeTextField looses focus, you don't have that control using JTimeTextField. But you can instantiate the desired adapter and attach it to JTextField.

Using JTimeTextField

JTimeTextField accepts either Base12, Base24 or both of the time
formats. The acceptable format can be specified when instatiating, or
can be set using setFilter method.

1. BASE24 - Accepts time in base-24 format (HH:MM:SS.SSS)
2. BASE12 - Accepts time in base-12 format (HH:MM:SS.SSSxx) here
AM/PM must be specified. Otherwise input is treated invalid.
3. ANY - Accepts time in any format. This format allows time
without AM/PM also.

JTimeTextField has four constructors -

1. JTimeTextField() - Accepts any time format
2. JTimeTextField( type ) - Accepts specified format (BASE24/BASE12/ANY)
3. JTimeTextField( type, Calendar ) - Same as above, but sets the time of the field initially.
4. JTimeTextField( type, Date ) - Same as above, provided for backword compatibility.

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

Code below shows -

1. how to use JTimeTextField
2. how to gain control on invalid time input using TimeListener 3. how to use the adapters individually


/**
 * @(#) TestPanel.java	1.0	99/06/11
 *
 * 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	um.filteredtext.*;
import	um.filteredtext.time.*;
import	um.event.*;
import	java.awt.*;
import	java.awt.event.*;
import	java.util.*;

/**
 * Test application for testing time field.
 *
 * @version	1.0	06/11/99
 * @author	UnicMan
 */
public class TestPanel
	extends		JPanel
	implements	WindowListener,
				TimeListener
{
	public static final boolean	DEBUG = true;

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

		////
		// Test components for testing 24 based time filter ...
		////
		JTimeTextField	ttfBase24	= new JTimeTextField(JTimeTextField.BASE24);
		TextField	tfBase24		= new TextField();
		Base24TimeAdapter taBase24	= new Base24TimeAdapter();

		ttfBase24.setTimeListener( this );
		tfBase24.addKeyListener( taBase24 );
		tfBase24.addFocusListener( taBase24 );
		// Add all short time components ...
		add( new JLabel("24 clock:") );
		add( ttfBase24 );
		add( tfBase24 );

		////
		// Test components for testing 12 based time filter ...
		////
		JTimeTextField	ttfBase12	= new JTimeTextField(JTimeTextField.BASE12);
		TextField	tfBase12		= new TextField();
		Base12TimeAdapter taBase12	= new Base12TimeAdapter();

		ttfBase12.setTimeListener( this );
		tfBase12.addKeyListener( taBase12 );
		tfBase12.addFocusListener( taBase12 );
		// Add all short time components ...
		add( new JLabel("12 clock:") );
		add( ttfBase12 );
		add( tfBase12 );

		////
		// Test components for testing any time filter ...
		////
		JTimeTextField	dtfAny	= new JTimeTextField(JTimeTextField.ANY);
		TextField	tfAny		= new TextField();
		AnyTimeAdapter daAny	= new AnyTimeAdapter();

		dtfAny.setTimeListener( this );
		tfAny.addKeyListener( daAny );
		tfAny.addFocusListener( daAny );
		// Add all short time components ...
		add( new JLabel("Any time:") );
		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 gotInvalidTime( TimeEvent deEvt )
	{
		System.out.println( "Got dirty time: " + deEvt.getInvalidText() );
	}
}



Download JDK1.2 demo
Download JDK1.1.5 demo

Download TimeFilter source code
Download common source code

Posted On: 25-Jun-1999

internet.commerce



Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers