实施 DocumentFilter 时 Enter 键不起作用

发布于 2024-12-20 22:25:08 字数 1273 浏览 1 评论 0原文

我通过继承原始的 DocumentFilter 类并重写它的 insertreplace 方法来实现文档过滤器。它响应除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2024-12-27 22:25:08

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

彡翼 2024-12-27 22:25:08

我的意思是如何使用 keyListener 或 actionListener 使插入符号移动到下一行?

默认情况下,按下 Enter 键时会在文档中插入换行符。

如果您不喜欢这种行为,那么您需要将默认操作替换为您自己的自定义操作,将插入符号放在下一行的开头。

请阅读按键绑定,了解有关如何执行此操作的更多信息。创建自定义操作时,您应该能够使用文本实用程序 类来帮助您将克拉定位在下一行。

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.

街道布景 2024-12-27 22:25:08

尝试使用 addKeyListener() 并手动处理 Enter 键

Try to use addKeyListener() and handle Enter key manually

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文