Results 1 to 7 of 7
Thread: ignore backspace
- 05-16-2010, 04:27 AM #1
ignore backspace
i am creating a program that you would enter a number and it converts it. I added the try-catch block so it would display a error if you entered something else than a number. the only thing is that when the user enters backspace the same error comes up. how do you ignore the backspace key? also i am using key-listener.
-
- 05-16-2010, 05:14 AM #3
since i never used a DocumentFilter before i don't know anything about it. i also looked at the link and it helped a little but i have a few questions.
so do i put a Document Filter on a JTextfield. And would i use the replace method to remove the last character when the backspace key is pressed?
-
You'd add a DocumentFilter to the Document (PlainDocument) held by the JTextField, not the JTextField itself, and it should allow you to cleanly accept only numeric input. If your goal is to mainly ignore the backspace key, then I believe that Key Binding would solve this best. There's a tutorial for this too.
Best of luck.
- 05-16-2010, 06:03 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
No, you don't do anything.And would i use the replace method to remove the last character when the backspace key is pressed?
The "replace" method is called when you try to insert a String into the Document. So you edit the string for valid characters.
The "remove" method is called when you use the backspace or delete key. So there is nothing special to code as the character is removed for you.
Try the example code found in the Swing tutorial.
- 05-16-2010, 06:57 PM #6
i am going to use document filter but i still need to use keylistener.
this is the code:
every time you type in a hex it gets the text and converts it.Java Code:public void keyTyped(KeyEvent k) { if(k.getSource()==hx.hex) { try{ char key = k.getKeyChar(); long chex = Long.parseLong(hx.hex.getText() + key, 16); hx.dec.setText(Long.toString(chex)); }catch(NumberFormatException chex) { JOptionPane.showMessageDialog(null, hmess, "Input error", JOptionPane.ERROR_MESSAGE); }
- 05-16-2010, 09:34 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
ignore links when building file tree
By itaipee in forum New To JavaReplies: 1Last Post: 07-28-2009, 06:41 PM -
*TEST* --ignore this--
By angryboy in forum Reviews / AdvertisingReplies: 5Last Post: 05-01-2009, 08:15 AM -
Can't make regex ignore line terminator
By mobilityguy in forum New To JavaReplies: 2Last Post: 03-11-2009, 04:27 PM -
Ignore Symbol
By Xystus777 in forum New To JavaReplies: 2Last Post: 01-21-2009, 12:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks