I'm writing a hex editor-like view, which is composed of two JTextComponents (hex and ASCII). I'd like to synchronize the selection between the two views, so I've implemented a CaretListener for the two components. This works well for responding to selection events where the user has long-pressed, dragged, and released the mouse. The components receive the caretUpdate when the user releases the mouse.
How can the components receive incremental caretUpdate events as the user presses the mouse and drags the mouse around without releasing?
There is, unfortunately, no selection model for
JTextArea
, otherwise this would have being really easy...Instead, I was forced to add a
ChangeListener
to theCaret
of each text area. This allowed me to see when the caret position was changed in real time.The next problem occurred when I realised that only the current text area would actually show it's selection highlight's....(nb This can be easily rectified, check next update)
I had to then apply a highlighter to the unfocused text area...
ps- You'll need to supply your own text ;)
Update with "non-highlighter" example
Thanks to StanislavL for pointing out that you can use
JTextComponent#getCaret()#setSelectionVisible(true)
to make a non focused text component show it's selected text.I did find that focus changes made this
false
again, so I've updated within the the change handler to always betrue