java 在允许可见光标移动的同时禁用 JTextPane 中的编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12700607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:53:41  来源:igfitidea点击:

Disable editing in a JTextPane while allowing visible cursor movement

javaswingjtextpane

提问by Gigatron

I have a JTextPanewhich is populated by reading from a file, after which the data is parsed and formatted. The user is not allowed to edit the JTextPane, but I want them to be able to navigate in it with a visible cursor.

我有一个JTextPane通过从文件中读取来填充的,然后解析和格式化数据。不允许用户编辑JTextPane,但我希望他们能够使用可见光标在其中导航。

If I use setEditable(false), the cursor is invisible, although it is possible to indirectly observe the position of the invisible cursor by holding down Shiftand using the arrow keys to select a block of text.

如果我使用setEditable(false),光标是不可见的,尽管可以通过按住Shift并使用箭头键选择文本块来间接观察不可见光标的位置。

To enable a visible cursor while disallowing editing, instead of setEditable(false)I created a dummy DocumentFilterthat simply does nothing for its insertString(), remove(), and replace()methods. But then I have to swap in a regular filter in order to programmatically populate the JTextPanefrom a file, then put back the dummy filter right before returning control to the user.

要启用一个可见光标同时禁止编辑,而不是setEditable(false)我创建了一个虚拟的DocumentFilter,根本无助于它的insertString()remove()replace()方法。但随后我必须交换常规过滤器,以便以编程方式JTextPane从文件中填充,然后在将控制权返回给用户之前放回虚拟过滤器。

So far this seems to work, but is there a simpler solution? If I leave this as is, is there any sequence of keystrokes or mouse activity that could somehow allow the user to edit the text pane, given that it is technically editable as per setEditable?

到目前为止,这似乎有效,但是有更简单的解决方案吗?如果我保持原样,是否有任何按键或鼠标活动序列可以以某种方式允许用户编辑文本窗格,因为它在技术上是可编辑的setEditable

回答by Bruno Vieira

textPane.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            textPane.setEditable(true);

        }

        @Override
        public void focusGained(FocusEvent e) {
            textPane.setEditable(false);

        }
    });

Yet another dirty hack! It seems to provide what you need!

又一个肮脏的黑客!它似乎提供了您需要的东西!

回答by StanislavL

Extend your DocumentFilter introducing flag isAPI. If it's false prevent all the changes. When you need to add content programmatically set the flag, add the content, and reset the flag back to false.

扩展您的 DocumentFilter 引入标志 isAPI。如果为 false,则阻止所有更改。当您需要以编程方式添加内容时,设置标志,添加内容,并将标志重置回 false。

回答by Bram Janssens

This did the trick for me (a combination of the previous suggestions):

这对我有用(结合了之前的建议):

textPane.addFocusListener(new FocusListener() {

    @Override
    public void focusGained(FocusEvent e) {
         textPane.getCaret().setVisible(true);
    }
});

回答by MadProgrammer

This is probably a dirty, dirty hack, but I got this to work.

这可能是一个肮脏的、肮脏的黑客,但我让它起作用了。

After you have set the text pane to un-editable, use textPane.getCaret().setVisible(true)to re-enable the caret

将文本窗格设置为不可编辑后,使用textPane.getCaret().setVisible(true)重新启用插入符号