Search code examples
javaswingselectionjtextpane

How to get selection from JTextPane


I want to find out which part of JTextPanel text is selected. Tried to call JTextPane.getSelectionStart() and JTextPane.getSelectionEnd(), but they always return same value that is equal to current caret position. What is my problem with that?

I would be thankful for any code exapmle that gets current selection.


Solution

  • I found my problem - that was a custom FocusListener which was changing JTextPane content (and so dropping the selection) before I got the keyTyped event.

    Anyway, thanks everyone for examples and comments!