在允许可见光标移动的同时,禁用JTextPane中的编辑 [英] Disable editing in a JTextPane while allowing visible cursor movement

查看:53
本文介绍了在允许可见光标移动的同时,禁用JTextPane中的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JTextPane which 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.

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

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 Shift and using the arrow keys to select a block of text.

为了在禁止编辑时启用可见光标,而不是setEditable(false),我创建了一个虚拟DocumentFilter,该虚拟DocumentFilter对其insertString()remove()replace()方法完全不执行任何操作.但是然后我必须交换常规过滤器,以便以编程方式从文件填充JTextPane,然后在将控制权返回给用户之前放回虚拟过滤器.

To enable a visible cursor while disallowing editing, instead of setEditable(false) I created a dummy DocumentFilter that 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 JTextPane from a file, then put back the dummy filter right before returning control to the user.

到目前为止,这似乎可行,但是有没有更简单的解决方案?如果我将其保留原样,是否存在任何顺序的击键或鼠标活动,可以允许用户编辑文本窗格,前提是该窗格在技术上可以按照setEditable进行编辑?

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?

推荐答案

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

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.

这篇关于在允许可见光标移动的同时,禁用JTextPane中的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆