实施 DocumentFilter 时 Enter 键不起作用
我通过继承原始的 DocumentFilter
类并重写它的 insert
和 replace
方法来实现文档过滤器。它响应除 Enter 键之外的所有键。我的意思是,当我按 Enter 时,它应该转到我的 JTextPane
中的下一行,但它没有这样做。那么如何才能让我的Enter
键正常工作呢?
代码
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);
}
}
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Translate.translateToUrdu(char)
对输入键字符 (13) 有何作用?这似乎是问题所在,因为您说您不使用
ActionListener
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
默认情况下,按下 Enter 键时会在文档中插入换行符。
如果您不喜欢这种行为,那么您需要将默认操作替换为您自己的自定义操作,将插入符号放在下一行的开头。
请阅读按键绑定,了解有关如何执行此操作的更多信息。创建自定义操作时,您应该能够使用文本实用程序 类来帮助您将克拉定位在下一行。
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.
尝试使用
addKeyListener()
并手动处理 Enter 键Try to use
addKeyListener()
and handle Enter key manually