This example displays the text of the selected range.
System.out.println(document.getSelection().getRange().getText());
Related information
Range property