Tuesday 8 April 2014

Today You will learn :

  • How to use Inner Class to use Key Events and Mouse Events
  • How to cancel key typed event in Java
  • How make numeric JTextField
  • How to limit the number of characters in JTextField
  • consume() method of the KeyListener interface
  • Character.isDigit() method and getKeyChar() method

The main things here are the JTextField , KeyEvents , Inner Classes in Java and how to create numeric TextFields in Java and if you want numeric text box in C# Click Here 

How to use Inner Classes to Implement KeyEvents?

 When i first used inner classes i really avoided them because they a bit complex. But trust me Inner classes are'nt that difficult as they are thought to be. Here's how its done :
  • Create a private inner class
  • Use the implement keyword to implement the KeyListener interface
  • Override all the methods of the interface KeyListener
  • Create the Object of the Inner Class in the enclosing Class like that "InnerClass i = outerclassobject.new InnerClass()"(you have to create outer class object first)
  • Add the object of inner class to the object of component class which you want to implement KeyEvents for example "JTextField.add(KeyListenerObject)"

Here is the code:


 import javax.swing.*;  
 import java.awt.event.KeyEvent;  
 import java.awt.event.KeyListener;  
   
 public class NumericBox{  
      static JTextField tf = new JTextField();  
        
      public static void main(String arh[]){  
             
           NumericBox g = new NumericBox();  
           Handle h = g.new Handle();  
           JFrame f = new JFrame();  
           JPanel p = new JPanel();  
             
           tf.setColumns(5);  
           tf.addKeyListener(h);  
             
           p.add(tf);  
             
           f.add(p);  
           f.setSize(200, 200);  
           f.setVisible(true);  
             
      }//end of main method  
        
      private class Handle implements KeyListener{  
   
       public void keyTyped(KeyEvent e) {  
           if(tf.getText().length() == 6)  
                     e.consume();  
                       
           if(e.getKeyChar() != 13 && !Character.isDigit(e.getKeyChar())   
                && e.getKeyChar() != 42 && e.getKeyChar() != 43   //key codes of the keys
                && e.getKeyChar() != 45 && e.getKeyChar() != 46   //you want to enable
                && e.getKeyChar() != 47)  
                     e.consume();            
           }  
   
           public void keyPressed(KeyEvent e) {}  
           public void keyReleased(KeyEvent e) {}  
             
      }//end of inner class  
        
 }//end of outer class  
   
   

Output:

Explanation : 

                   In the keyTyped method of the inner class at  I have checked if the the characters typed in the text field is equal to 6 then the key event is canceled by using the consume() method in the this way i have restricted from entering more than 6 character in the text field.  To restrict the user to enter digits only i have used Character.isDigit(e.getkeyChar()). e.getKeyChar() returns the ASCII code of the key pressed here if the key pressed do not represent a digit the key event is canceled similarly i have enabled several other keys using ASCII code if you don't know the ASCII code of keyboard keys CLICK HERE.

I hope You have learned Something from this post if have any suggesions for me contact me at daniequreshigmail.com or comment below And Follow Me for more stuff!!!



0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!