Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
can't get rid of 1st typed character
Gary Frick
Greenhorn
Posts: 28
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to limit input to only numerics, but when I try to stop the character from appearing, I am only successful with characters beyond the first one. I can't figure out why the event is getting through and the first character always remains.
public class keyCheckHandler implements KeyListener{ public void keyPressed(java.awt.event.KeyEvent keyEvent) { int key = keyEvent.getKeyCode(); boolean badvalue = false; JTextField keysrc; keysrc = (JTextField)keyEvent.getComponent(); keysrc.getClass(); switch(key) { case KeyEvent.VK_BACK_SPACE: break; case KeyEvent.VK_DELETE: break; default: { if (( key < 48) ^ (key > 57)) { badvalue = true; keyEvent.consume(); } break; } } if (badvalue) { if( keysrc.getText().length() > 0) keysrc.setText(keysrc.getText().substring(0, keysrc.getText().length() - 1)); else keysrc.setText(null); } }
Thanks
alagan sathianathan
Greenhorn
Posts: 18
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Why dont you try using Document class:
IntegerField allows only numeric fields
public class IntegerField extends JTextField { public int constraint; public IntegerField(int i, int j) { super(i); constraint = j; detDocument(new NumericDocument(constraint)); } }
Document class:
import javax.swing.text.*; public class NumericDocument extends PlainDocument { public int constraint; protected NumericDocument(int i) { constraint = i; } public void insertString(int i, String s, AttributeSet attributeset) throws BadLocationException { if(s == null) { return; } char ac[] = s.toCharArray(); boolean flag = true; boolean flag1 = true; if(ac.length + getLength() <= constraint) { for(int j = 0; j < ac.length; j++) { if(Character.isDigit(ac[j])) { continue; } flag = false; break; } } else { flag1 = false; } if(flag1 && flag) { super.insertString(i, s, attributeset); } } }
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
June Newsletter Puzzle
Robot - can't get symbols: !@#$^&*()_+
u_int8_t in java
Probelm with KeyListener
automatically update score when user input in JTextField..
More...