Deselect selected text in JTextPane

拟墨画扇 提交于 2021-02-20 11:23:44

问题


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?


回答1:


Text selection is temporaray and contiguous. You can't unselect some text in the middle of a larger string of selected text.

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




回答2:


I am not sure, but try this method.

textPane.setCaretPosition(start);



回答3:


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);


来源:https://stackoverflow.com/questions/6744028/deselect-selected-text-in-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!