Results 1 to 2 of 2

Thread: I need help with KeyEvent registered to a JTextField

  1. #1

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    I need help with KeyEvent registered to a JTextField

    I have a textfield which has the focus and has a KeyListener... The user can click any key on the keyboard... But some key's like the '=' key has a special function (a dialog will open)... How do I go about this having to open the dialog while the key is not explicitly placed in the JTextField (much like that of the Cancel key of Visual Basic where the key is not rendered to the field...)

    is it enough to just do the following code?
    ke.setKeyChar(KeyEvent.UNDEFINED_CHAR);
    or
    ke.setKeyCode(KeyEvent.VK_Cancel);

    Please I need help

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I can't see anyway of doing this because i don't think you can add a key listener to any component that dosen't listen for KeyEvents. If you are testing to see if a value entered in a JTextField equals = then the following code works.

    Code:
     import java.awt.*;
     import java.awt.event.*; 
     import javax.swing.*; 
    
     public class W{
       public static void main(String[] agrs){
    
       final JFrame jf = new JFrame();
       
       JTextField jtf = new JTextField(10); 
       jf.getContentPane().add(jtf,BorderLayout.NORTH); 
     
       jf.setSize(300,400);
       jf.setVisible(true);
       jtf.addKeyListener(new KeyGrabber()); 
       jf.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent we){
         System.exit(0);
       }
      }); 
      }
     }
    
     class KeyGrabber extends KeyAdapter{
       public void keyTyped(KeyEvent ke){
        if('=' == ke.getKeyChar()){
          // pop up dialog
        }
       }
     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width