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:
consume ( ) method does work with JTextField
Posted by Kishori Sharan on January 02, 2001 at 2:31 PM
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:
- Thanx ruchi and hema January 04, 2001 at 8:46 AM
(0)
|