当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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

网友答案:

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.

网友答案:

Try to use addKeyListener() and handle Enter key manually

分享给朋友:
您可能感兴趣的文章:
随机阅读: