How can I listen for changes to the text selection in a JTextArea?

John Zukowski

You can attach a CaretListener to a JTextComponent to find out when the caret moves. When the dot and mark properties of the CaretEvent are the same, it means nothing is selected. You have to go back to the JTextComponent to find out the exact text that is selected.