The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Thanx

Posted by ruchi and hema on January 04, 2001 at 8:46 AM

> Try the following Test.java. The consume () method works and it lets you type only digits ( 0 thru 9 ) .

> ///////////////////Test.java

> import java.awt.* ;
> import java.awt.event.* ;
> import javax.swing.*;

>
> public class Phone extends JFrame {
> JTextField phone = new JTextField ( ) ;
>
> public Phone ( ) {
> Container cp = getContentPane ( ) ;
> cp.add ( phone, "North" ) ;
> phone.addKeyListener (
> new KeyAdapter ( ) {
> public void keyTyped ( KeyEvent e ) {
> int key = e.getKeyChar() ;
>
> // Also allow Delete and BackSpace keys to work
> if ( !( ( key >= 48 && key <= 57 ) || ( key == KeyEvent.VK_DELETE ) || ( key == KeyEvent.VK_BACK_SPACE ) ) ) {
> e.consume ( ) ;
> }
>
> }
> }
> ) ;
> }
>
> public static void main ( String[] args ) {
> Phone p = new Phone ( ) ;
> p.setBounds ( 20, 20 , 200 , 200 ) ;
> p.setVisible ( true ) ;
> }
> }






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us