Results 1 to 6 of 6

Thread: Highlighting Words in a JTextComponent in Java

  1. #1
    Join Date
    Jan 2009
    Posts
    61

    Highlighting Words in a JTextComponent in Java

    Hello, I am working on the project in java. And while working on it, I want to do something from which I will able to highlight the word which are present in the JTectComponent. If anyone knows logic for doing this, then also please help me to achieve it. If you are having any reference material to get the details about it, then also help me to achieve it. I will be thankful to you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Highlighting Words in a JTextComponent in Java

    Hello, if you want to highlight the words in a JTextComponenet in java then you can able to solve the problem with the help of code below. So, just make use of it and you will able to highlight the words in a text component.
    Code:
    JTextArea ta = new JTextArea(); 
    highlight(ta, "public"); 
    public void highlight(JTextComponent ta, String str) { 
    removeHighlights(ta); 
    try 
    { 
    Highlighter highlighter = ta.getHighlighter(); 
    Document document = ta.getDocument(); 
    String text = document.getText(0, document.getLength()); 
    int pos = 0;
    while ((pos = text.indexOf(str, pos)) >= 0) 
    { 
    highlighter.addHighlight(pos, pos+str.length(), myHighlightPainter); 
    pos += str.length(); 
    } 
    }
     catch (BadLocationException e) 
    { 
    } 
    }  
    public void removeHighlights(JTextComponent ta) 
    { 
    Highlighter highlighter = ta.getHighlighter();
    Highlighter.Highlight[] highlighters = highlighter.getHighlights();
    for (int i=0; i<highlighters.length; i++) 
    { 
    if (highlighters[i].getPainter() instanceof MyHighlightPainter) 
    { 
    highlighter.removeHighlight(highlighters[i]); 
    } 
    } 
    } 
    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); 
    class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter 
    { 
    public MyHighlightPainter(Color color) 
    { 
    super(color); 
    } 
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Highlighting Words in a JTextComponent in Java

    Hello, if you want to do it, then you need to make use of the text component's highlighter for creating and also controlling the highlights. As there is one selection highlights present into the highlighter is the selection so it is needed to take care while making use of it. If you simply make use of the private painter for creating the highlights. So, while removing the highlights you can simply use the object of the highlighter for removing the highlights. And if you want to use different highlighting styles then you need to use the styles with the JTextPane.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Highlighting Words in a JTextComponent in Java

    Hey, I have got the code below which I think help you for highlighting words in a JTextComponent in java. So, just use it and get your problem solved:
    Code:
    private void localizarButtonMouseClicked(java.awt.event.MouseEvent evt) {                                             
        String str = ta1.getText();
        String str2 = t1.getText();
        Highlighter.HighlightPainter highlightpainter = new MyHighlightPainter(Color.yellow);
        Highlighter highlighter = ta1.getHighlighter();
        highlighter.removeAllHighlights();
        
        int position = 0;
        if(str2.length() > 0)
     {
            while(( position = str.indexOf(str2, position)) >= 0) 
    {
                try
    {
                    highlighter.addHighlight(position,  position + str2.length(), highlightpainter);
                }
    catch (BadLocationException e) 
    {
                }
                position += str2.length();
            }
        }
    }
    class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter 
    {
            public MyHighlightPainter(Color clr) 
    {
                super(clr);
            }
        }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Highlighting Words in a JTextComponent in Java

    Hey, I have got the Word Highlighter 1.0.1 software application which is having following features:

    • Word Highlighter Software highlight multiple words in doc files .
    • Word Highlighter Tool Match Case and Whole Word option available.
    • This Word Processing Tool backup of original file before Process.
    • Word Highlighter Tool Process thousands of files.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Highlighting Words in a JTextComponent in Java

    Hello, if you use the code below then you will able to get the solution for your problem:
    Code:
    private void docupdate(DocumentEvent dcoumentupdate) {
            
            if(dcoumentupdate.getOffset() == 0) {
                boolean change = false;
                Highlighter.Highlight myHighlight = null;
                Highlighter.Highlight[] highlights = textPane.getHighlighter().getHighlights();
                
                for(int i=0; i < highlights.length; i++) {
                    myHighlight = highlights[i];
                    if(myHighlight.getStartOffset() == dcoumentupdate.getOffset()) {
                        change = true;
                        break;
                    }
                }
                
                if(change) {
                    Highlighter hl = textPane.getHighlighter();
                    
                    try {
                        hl.change(myHighlight, myHighlight.getStartOffset()+1, myHighlight.getEndOffset());
                    }
                    catch(BadLocationException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

Similar Threads

  1. Highlighting images on Acrobat 9 Pro
    By Jesus-Ernesto in forum Windows Software
    Replies: 5
    Last Post: 09-07-2010, 12:30 PM
  2. Visual Studios Syntax Highlighting
    By Xan Mathew in forum Software Development
    Replies: 6
    Last Post: 17-05-2010, 09:51 AM
  3. Disable highlighting new software in windows XP
    By Camdean in forum Operating Systems
    Replies: 4
    Last Post: 20-04-2010, 06:29 AM
  4. Breaking a String into Words In Java
    By Bhardwaj in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 05:21 PM
  5. Is anybody how to count number of words in MS Words?
    By Sonam Goenka in forum Windows Software
    Replies: 5
    Last Post: 21-01-2010, 02:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,721,055.38291 seconds with 16 queries