Highlighting few of the words of a text file opened in a frame

后端 未结 1 1957
南笙
南笙 2020-12-06 14:49

I am opening a file in a frame and I wish to highlight few of the words.As I understand,I need to traverse the content of the file. How do I traverse through the contents an

相关标签:
1条回答
  • 2020-12-06 15:23

    To highlight on a JEditorPane have a look at this example:

    enter image description here

    import java.awt.Color;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DefaultHighlighter;
    import javax.swing.text.Document;
    import javax.swing.text.Highlighter;
    import javax.swing.text.JTextComponent;
    
    public class Test {
    
        // An instance of the subclass of the default highlight painter
        static MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                    JEditorPane jep = new JEditorPane();
                    jep.setText("Hello to the public");
                    frame.add(jep);
                    frame.pack();
                    frame.setVisible(true);
    
                    highlight(jep, "public");//'public is the word to highligh'
    
                }
            });
        }
    
        // Creates highlights around all occurrences of pattern in textComp
        public static void highlight(JTextComponent textComp, String pattern) {
            // First remove all old highlights
            removeHighlights(textComp);
    
            try {
                Highlighter hilite = textComp.getHighlighter();
                Document doc = textComp.getDocument();
                String text = doc.getText(0, doc.getLength());
                int pos = 0;
    
                // Search for pattern
                while ((pos = text.indexOf(pattern, pos)) >= 0) {
                    // Create highlighter using private painter and apply around pattern
                    hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
                    pos += pattern.length();
                }
            } catch (BadLocationException e) {
            }
        }
    
        // Removes only our private highlights
        public static void removeHighlights(JTextComponent textComp) {
            Highlighter hilite = textComp.getHighlighter();
            Highlighter.Highlight[] hilites = hilite.getHighlights();
    
            for (int i = 0; i < hilites.length; i++) {
                if (hilites[i].getPainter() instanceof MyHighlightPainter) {
                    hilite.removeHighlight(hilites[i]);
                }
            }
        }
    }
    
    class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
    
        public MyHighlightPainter(Color color) {
            super(color);
        }
    }
    
    0 讨论(0)
提交回复
热议问题