Muhammad Salman Farooq
Muhammad Salman Farooq

Reputation: 1325

Enter Key not working while implementing DocumentFilter

I have implemented a document filter by inheriting the orignal DocumentFilter class and overriding it's insert and replace methods. It is responding to all keys except the Enter key. I mean when ever I press Enter, it should go to the next line in my JTextPane but it is not doing that. So how can I make my Enter key work properly?

Code

class UrduFilter extends DocumentFilter {
//My urdu filter overriding insertString and replace
    char urduChar;
    String urduString;

    public void insertString(DocumentFilter.FilterByPass fb, int offset,
            String text, AttributeSet attr) throws BadLocationException {
        System.out.println("\n" + text);
        urduChar = Translate.translateToUrdu(text.charAt(0));
        urduString = Character.toString(urduChar);
        fb.insertString(offset, urduString, attr);
    }

    //no need to override remove(): inherited version allows all removals

    public void replace(DocumentFilter.FilterByPass fb, int offset, int length, 
            String text, AttributeSet attr) throws BadLocationException {
        urduChar = Translate.translateToUrdu(text.charAt(0));
        System.out.println(text + " ... " + text.charAt(0));
        urduString = Character.toString(urduChar);
        fb.replace(offset, length, urduString, attr);
    }
}

Thanks.

Upvotes: 0

Views: 404

Answers (3)

camickr
camickr

Reputation: 324197

I mean how can i make the caret to move to next line either with keyListener or actionListener?

By default a newline character is inserted into the Document when the Enter key is pressed.

If you don't like this behaviour then you need to replace the default Action with a custom Action of your own that places the Caret at the beginning of the next line.

Read up on Key Bindings for more information on how to do this. When you create your custom Action you should be able to use the Text Utilities class to help you position the Carat on the next line.

Upvotes: 3

ring bearer
ring bearer

Reputation: 20803

What does Translate.translateToUrdu(char) do with enter key character ( 13 ) ? That seems to be the issue since you say that you do not use an ActionListener

Upvotes: 3

korifey
korifey

Reputation: 3519

Try to use addKeyListener() and handle Enter key manually

Upvotes: -2

Related Questions