I need to make an application which copies the highlighted text .
I tried this program
String myString = "This text will be copied into clipboard when running this code!";
StringSelection stringSelection = new StringSelection (myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
clpbrd.setContents (stringSelection, null);
but this copies the text already existing in clipboard
it prints the text from clipboard after we highlight the text and hit copy option
I want to know after we simply select a text by highlighting, where is this stored, how can I access this highlighted text directly without physically copying it by OS’s copy option
You can listen to selection changes using the
CaretListener. Then you can retrieve the selected Text using the#getSelectedText().Here is a simple example with a
TextArea: