Results 1 to 8 of 8
- 08-24-2011, 06:38 AM #1
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....
-
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).
- 08-24-2011, 07:03 AM #3
OK, But how to use DocumentListener? I don't See any method like addDocumentListener() on the reference of JTextField...
- 08-24-2011, 07:09 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Add it to the Document of the text component.
- 08-24-2011, 07:14 AM #5
Is it the setDocument(Document doc) method?
- 08-24-2011, 07:23 AM #6
-
You call getDocument() on the JTextField to get its document and then you add the DocumentListener to that Document.
- 08-24-2011, 10:13 AM #8
Similar Threads
-
A problem with KeyListener
By Reskaillev in forum New To JavaReplies: 4Last Post: 07-22-2011, 02:29 AM -
Help with KeyListener in Java Swing
By codeRunner1 in forum AWT / SwingReplies: 1Last Post: 02-27-2011, 11:48 PM -
Problem with Keylistener, some help pls
By syon in forum AWT / SwingReplies: 1Last Post: 01-21-2011, 01:31 AM -
AWT KeyListener Problem
By plm-pusik in forum New To JavaReplies: 15Last Post: 11-10-2010, 03:38 PM -
KeyListener problem
By siyi90 in forum AWT / SwingReplies: 7Last Post: 02-08-2010, 10:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks