Results 1 to 8 of 8
  1. #1
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Unhappy Problem Using KeyListener in swing program.

    Hi guys I am creating a Swing application, but having a problem in using keyListener.
    I am building a JFrame containing a JDesktopPane. This JDesktopPane will show three JInternalFrame. This programe uses a Sqlite embedded database. The InternalFrames is used For
    1. Displaying Data
    2. Inserting Data
    3. Updating Data

    In the Update Internal frame I have a JTextField "sno" on which I applied the KeyListener.
    What I require is that when I type the Integers in this Jtextfield, the matching Serial No should
    match in the database and rest of the text boxes should automatically fill up.
    The Code I have written is this:
    Java Code:
    @Override
    public void keyTyped(KeyEvent e){		
           	if(winUp != null){
    		if(e.getSource().equals(winUp.tsno)==true){
    			char ch = e.getKeyChar();
    			String sno = winUp.tsno.getText();  /****This is where the problem lies****/
    			if((ch>32 && ch < 48) || (ch > 57 && ch < 126)){					
    				JOptionPane.showMessageDialog(winUp,MSG,"Wrong input",JOptionPane.WARNING_MESSAGE);
    				e.consume();
    			}
    			else{
    				if(sno != null){
    					if(ActionHandle.validSerial(sno)==true){
    						Game g = DBIntf.find(sno);
    						if(g != null){
    							setData(g);
    							winUp.snoUp = sno;
    						}
    						else{
    							ActionHandle.clearUpdate(winUp);
    						}
    					}						
    				}			
    			}
    		}
    	}
    }

    The problem in this code is where I marked in Comment. That is the getText() method of the JTextField is not providing the text in the TextField.
    When I Type one more character then the text before this typed character is returned by the TextField.

    I am puzzled. Please help....

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't use a KeyListener in this situation. Much more appropriate would be a DocumentListener if you're not going to modify the text entered into the JTextField or a DocumentFilter if you are going to modify the text (or allow vs disallow some text).

  3. #3
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default

    OK, But how to use DocumentListener? I don't See any method like addDocumentListener() on the reference of JTextField...

  4. #4
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Add it to the Document of the text component.

  5. #5
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default

    Is it the setDocument(Document doc) method?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by gauravfzd View Post
    Is it the setDocument(Document doc) method?
    To do what?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You call getDocument() on the JTextField to get its document and then you add the DocumentListener to that Document.

  8. #8
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

Similar Threads

  1. A problem with KeyListener
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 07-22-2011, 02:29 AM
  2. Help with KeyListener in Java Swing
    By codeRunner1 in forum AWT / Swing
    Replies: 1
    Last Post: 02-27-2011, 11:48 PM
  3. Problem with Keylistener, some help pls
    By syon in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2011, 01:31 AM
  4. AWT KeyListener Problem
    By plm-pusik in forum New To Java
    Replies: 15
    Last Post: 11-10-2010, 03:38 PM
  5. KeyListener problem
    By siyi90 in forum AWT / Swing
    Replies: 7
    Last Post: 02-08-2010, 10:16 AM

Posting Permissions

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