簡體   English   中英

在JEditorPane中突出顯示一個單詞

[英]Highlight a word in JEditorPane

我必須突出顯示JEditorPane中一個單詞的所有出現。 為此,我使用以下代碼:

 try
{          
javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
    new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
textPane.getHighlighter().addHighlight(startPos, endPos, 
highlightPainter);
}
catch(Exception ex)
{
}

但是我怎樣才能給出一個詞的索引位置呢?

我正在從文件中讀取內容,但它也在讀取HTML標簽,並且正在干擾單詞索引。

基本上,您應該能夠遍歷文檔以尋找所需的匹配項...

在此處輸入圖片說明

public class TestEditorPane01 {

    public static void main(String[] args) {
        new TestEditorPane01();
    }

    public TestEditorPane01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JEditorPane editor = new JEditorPane();
                try {
                    editor.setPage(new File("Test.html").toURI().toURL());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(editor));
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Document document = editor.getDocument();
                try {
                    String find = "Method";
                    for (int index = 0; index + find.length() < document.getLength(); index++) {
                        String match = document.getText(index, find.length());
                        if (find.equals(match)) {
                            javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter =
                                    new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
                            editor.getHighlighter().addHighlight(index, index + find.length(),
                                    highlightPainter);
                        }
                    }
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }

            }
        });
    }
}

這將遍歷整個文檔並突出顯示所有匹配項。 這也是區分大小寫的匹配;)

這是我的情況,需要在EditorPane中突出顯示一個搜索詞:

    // text in EditPane
    String text = rSyntaxTextArea.getText();
    if (text != null && !"".equals(filterText.getText())) {
        Highlighter hilit = new RSyntaxTextAreaHighlighter();
        rSyntaxTextArea.setHighlighter(hilit);  
        for (int index = text.toUpperCase().indexOf(
                // searched text
                filterText.getText().toUpperCase()); index >= 0; index = text
                .toUpperCase().indexOf(
                        filterText.getText().toUpperCase(), index + 1)) {
            int end = index + filterText.getText().length();
            HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(
                    Color.LIGHT_GRAY);
            try {
                hilit.addHighlight(index, end, painter);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }

希望能有所幫助。

您可以執行以下操作:

getHighlighter().addHighlight(start, end, 
         new DefaultHighlighter.DefaultHighlightPainter(Color.red));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM