Author: UnicMan
Author's WebSite: http://members.tripod.com/unicman
Use of the component:
JNumberTextField accepts numbers from the user. It will ignore other keys and generate an event if invalid number is entered. Also the number filters are kepts separate so that they can be used individually. The allowed formats as number are:
1. NNNNN / N,NNN,NNN (Number)
2. NNNN.NNNNNN / N,NNN,NNN.NNNNNN (Real)
3. $NNNN.NN / $N,NNN,NNN.NN (Currency)
4. N.NNeNN (Scientific)
Design of DateFilter
JNumberTextField
|
+---[Uses]-- IntegerNumberAdapter -----\
| |
+---[Uses]-- RealNumberAdapter --------+----[Extends]---- NumberAdapter
| |
+---[Uses]-- CurrencyNumberAdapter ----|
| |
\---[Uses]-- ScientificNumberAdapter --/
As shown above JNumberTextField uses three adapters for filtering corresponding number keys and validating them. JNumberTextField also has supporting methods for setting and retrieving numbers.
NumberAdapter
|
+----[Implements]---- KeyListener
|
+----[Implements]---- InputMethodListener (only for JDK1.2 JTextField)
|
+----[Implements]---- FocusListener
|
\----[Uses]---- UMNumberFormat
All the adapters for number extend NumberAdapter. For compatibility with JDK1.1 and JDK1.2, it implements all of the three listenrs. 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 JNumberTextField looses focus, you don't have that control using JNumberTextField. But you can instantiate the desired adapter and attach it to JTextField.
UMNumberFormat is a interface for defining your custom number parser and formatter. There are two methods 'parseNumber' and 'formatNumber'. You can implement UMNumberFormat interface and attach it to any number adapter.
Using JNumberTextField
JNumberTextField accepts either integer, real or scientific numbers. The acceptable format can be specified when instatiating, or can be set using setFilter method.
1. NUMBER - Accepts integer (long to be precise) number
2. REAL - Accepts real numbers and will show 6 significant digits in the text field
3. CURRENCY - Accepts currency values. Only 2 significant digits are shown. The number will be preceeded by currency symbol.
4. SCIENTIFIC - Accept scientific numbers. The format for these numbers is like '10E2'.
JNumberTextField has three constructors -
1. JNumberTextField() - Accepts integers by default
2. JNumberTextField( type ) - Accepts specified format (NUMBER/REAL/CURRENCY/SCIENTIFIC)
3. JNumberTextField( type, Number ) - Same as above, but sets the number of the field initially.
If NumberListener is attached to the JNumberTextField, this field will generate event if invalid date is entered by the user. So control can be gained for invalid number (possibly for showing error messages etc). Only one NumberListener can be attached to one JNumberTextField.
Code below shows -
1. how to use JNumberTextField
2. how to gain control on invalid number input using NumberListener
3. how to use the adapters individually
/**
* @(#) TestPanel.java 1.0 99/07/04
*
* 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.number.*;
import um.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* Test application for testing number field.
*
* @version 1.0 07/04/99
* @author UnicMan
*/
public class TestPanel
extends JPanel
implements WindowListener,
NumberListener
{
public static final boolean DEBUG = true;
/**
* Default Constructor.
*/
public TestPanel()
{
setLayout( new GridLayout(4,3,5,5) );
////
// Test components for testing integer number filter ...
////
JNumberTextField ntfInteger = new JNumberTextField(JNumberTextField.NUMBER);
TextField tfInteger = new TextField();
IntegerNumberAdapter naInteger = new IntegerNumberAdapter(false);
ntfInteger.setNumberListener( this );
tfInteger.addKeyListener( naInteger );
tfInteger.addFocusListener( naInteger );
// Add all short time components ...
add( new JLabel("Integer:") );
add( tfInteger );
add( ntfInteger );
////
// Test components for testing real number filter ...
////
JNumberTextField rtfReal = new JNumberTextField(JNumberTextField.REAL);
TextField tfReal = new TextField();
RealNumberAdapter raReal = new RealNumberAdapter(false);
rtfReal.setNumberListener( this );
tfReal.addKeyListener( raReal );
tfReal.addFocusListener( raReal );
// Add all short time components ...
add( new JLabel("Real:") );
add( tfReal );
add( rtfReal );
////
// Test components for testing currency number filter ...
////
JNumberTextField ctfCurrency = new JNumberTextField(JNumberTextField.CURRENCY);
TextField tfCurrency = new TextField();
CurrencyNumberAdapter caCurrency = new CurrencyNumberAdapter(false);
ctfCurrency.setNumberListener( this );
tfCurrency.addKeyListener( caCurrency );
tfCurrency.addFocusListener( caCurrency );
// Add all short time components ...
add( new JLabel("Currency:") );
add( tfCurrency );
add( ctfCurrency );
////
// Test components for testing Scientific number filter ...
////
JNumberTextField stfScientific = new JNumberTextField(JNumberTextField.SCIENTIFIC);
TextField tfScientific = new TextField();
ScientificNumberAdapter caScientific = new ScientificNumberAdapter(false);
stfScientific.setNumberListener( this );
tfScientific.addKeyListener( caScientific );
tfScientific.addFocusListener( caScientific );
// Add all short time components ...
add( new JLabel("Scientific:") );
add( tfScientific );
add( stfScientific );
}
/**
* 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,130);
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 gotInvalidNumber( NumberEvent neEvt )
{
System.out.println( "Got dirty number: " + neEvt.getInvalidText() );
}
}
Download JDK1.2 demo
Download NumberFilter source code
Posted On: 3-Aug-1999