Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselect selected text in JTextPane

I have an editor in which I want to check that selected text will not contain some words. If it contains those particular words, then I need to deselect the selection made by user. Is there any way to do that in JTextPane?

like image 433
charmi Avatar asked Jul 19 '11 08:07

charmi


2 Answers

I am not sure, but try this method.

textPane.setCaretPosition(start);
like image 94
bugs_ Avatar answered Sep 28 '22 16:09

bugs_


int end = pane.getSelectionEnd();
pane.setSelectionStart(end);
pane.setSelectionEnd(end);

This will deselect the selected text and leave the caret at the end of whatever the user selected. It might pay to pop a JOptionPane telling the user why the selection disappeared..

JOptionPane.showMessageDialog(
    null, 
    "Don't select swear words!", 
    "Net Nanny says..", 
    JOptionPane.ERROR_MESSAGE);
like image 26
Andrew Thompson Avatar answered Sep 28 '22 15:09

Andrew Thompson