简体   繁体   English

取消选择 JTextPane 中的选定文本

[英]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 ?有没有办法在JTextPane中做到这一点?

I am not sure, but try this method.我不确定,但试试这个方法。

textPane.setCaretPosition(start);
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告诉用户选择消失的原因可能是值得的。

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

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().查看 API 以获得 getHighlighter()。 You can add/remove highlights and specify the start/end offsets of each highlight.您可以添加/删除高光并指定每个高光的开始/结束偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM