Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:40:33+00:00 2026-06-11T16:40:33+00:00

I am opening a file in a frame and I wish to highlight few

  • 0

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 and what is the related property that I may use for the purpose of highlighting?


UPDATE : MY CODE GOES SOMETHING LIKE THIS


private JEditorPane editorpane;
JScrollPane editorScrollPane;

public TextEditor()
{
    editorpane = new JEditorPane();
    editorpane.setEditable(false);

    if (filename != null)
    {
        try
        {
            File file = new File(filename);
            editorpane.setPage(file.toURI().toURL());
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.err.println("Attempted to read a bad file ...");
        }
    }
    else
    {
        System.err.println("File name is wrong");
    }

    add(editorpane);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:40:34+00:00Added an answer on June 11, 2026 at 4:40 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am opening a file which has 100,000 URL's. I need to send an
I'm opening a file and finding the line I need, but then I have
I am opening file in binary mode using fopen(abhi.zip,rb); and reading contents from it
After opening a file created by root in vim , how to change the
I am opening a file using the ACTION_VIEW intent from my app. Now i
I'm having trouble opening a file for reading, but only when it is inside
Simple question: I am opening a file in matlab 7.x, and I want to
What I mean by this, is opening a file with a Java code in
Opening an XML file in Internet explorer gives a security warning. IE has a
In Emacs 23.1 I'm opening up a file with conflict markers from a svn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.