Deselect text in JTextPane

I have an editor in which I want to check that the selected text will not contain some words. If it contains these specific words, then I need to undo the choices made by the user. Is there any way to do this in JTextPane?

+5
source share
3 answers

The selection of text is temporary and continuous. You cannot deselect text in the middle of a larger line of selected text.

Perhaps you are talking about backlighting. Check out the API for getHighlighter (). You can add / remove highlights and specify the start and end offsets of each highlight.

+2
source

I'm not sure, but try this method.

textPane.setCaretPosition(start);
+7
int end = pane.getSelectionEnd();
pane.setSelectionStart(end);
pane.setSelectionEnd(end);

Deselect the selected text and leave the caret at the end of what was selected by the user. He might pay pop JOptionPanetelling the user why the choice has disappeared.

JOptionPane.showMessageDialog(
    null, 
    "Don't select swear words!", 
    "Net Nanny says..", 
    JOptionPane.ERROR_MESSAGE);
+6
source

All Articles