Results 1 to 7 of 7
  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Question 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.

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

  3. #3
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    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?

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

    Default

    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.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    And would i use the replace method to remove the last character when the backspace key is pressed?
    No, you don't do anything.

    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.

  6. #6
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    i am going to use document filter but i still need to use keylistener.
    this is the code:
    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);
    			
    		}
    every time you type in a hex it gets the text and converts it.

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

    Default

    i am going to use document filter
    Good.

    but i still need to use keylistener.
    No you don't. Anything you can do with the keyTyped method you can do with a DocumentFilter

Similar Threads

  1. ignore links when building file tree
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 06:41 PM
  2. *TEST* --ignore this--
    By angryboy in forum Reviews / Advertising
    Replies: 5
    Last Post: 05-01-2009, 08:15 AM
  3. Can't make regex ignore line terminator
    By mobilityguy in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 04:27 PM
  4. Ignore Symbol
    By Xystus777 in forum New To Java
    Replies: 2
    Last Post: 01-21-2009, 12:24 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
  •