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.
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.
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.
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
?
insertString(), remove()
andreplace()
methods have a check for the flag, and depending on the flag they will either do nothing or pass on the instruction to the filter that was provided at construction time (or to the superclass if there was no such filter provided). – Scruffy