1

I am trying to highlight some code in a JEditorPane like this:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Driver
{
    public static void main(String[] args)
    {
        try
        {
            //create a simple frame with an editor pane
            JFrame frame = new JFrame("Highlight Test");
            JEditorPane pane = new JEditorPane();
            frame.getContentPane().add(pane);
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            //string to put in the pane
            String text = "1234567890";

            //grab the highlighter for the pane
            Highlighter highlighter = pane.getHighlighter();

            //store all the text at once
            pane.setText(text);

            //go through all the characters
            for(int i = 0; i < text.length(); i++)
            {
                //highlight the latest character
                highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

                //sleep for a quarter second
                Thread.sleep(250);
            }
        }catch(Exception ex){}
    }

}

This will highlight the characters one at a time and all the characters will remain highlighted. Now, I'd like the same behavior (all the characters remain highlighted) but I'd like to change the text in between highlights, like this:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Driver
{
    public static void main(String[] args)
    {
        try
        {
            //create a simple frame with an editor pane
            JFrame frame = new JFrame("Highlight Test");
            JEditorPane pane = new JEditorPane();
            frame.getContentPane().add(pane);
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            //string to put in the pane
            String text = "1234567890";

            //grab the highlighter for the pane
            Highlighter highlighter = pane.getHighlighter();

            //go through all the characters
            for(int i = 0; i < text.length(); i++)
            {
                //place a new string in the pane
                pane.setText(pane.getText() + text.charAt(i));

                //highlight the latest character
                highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

                //sleep for a quarter second
                Thread.sleep(250);
            }
        }catch(Exception ex){}
    }

}

Notice the text in the pane is changing and then I'm applying a new highlight. The old highlights go away- I'd like them to stay. My assumption is the highlights go away each time you setText(). So, is there any way to keep the highlights in the text component while changing the text?

Mark M
  • 1,807
  • 2
  • 21
  • 40

1 Answers1

1

i didn't tried the following code but what i suggest is just try to highlight both latest character and previous ones too like this:

        //go through all the characters
        for(int i = 0; i < text.length(); i++)
        {
            //place a new string in the pane
            pane.setText(pane.getText() + text.charAt(i));
          //highlight the previous characters
          if (i > 0) {
           for ( int j=i-1; j >= 0; j--)
             highlighter.addHighlight(j, j+1 , DefaultHighlighter.DefaultPainter);
           }
            //highlight the latest character
            highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

            //sleep for a quarter second
           // Thread.sleep(250);
        }
    }catch(Exception ex){}
othman
  • 4,428
  • 6
  • 34
  • 45
  • @othman this is Swing, and in Swing you'll never use Thread.sleep(int), because block EDT, with unexpected outPut to the GUI, please remove this code line from from your example – mKorbel Jun 16 '11 at 20:49
  • This highlights characters two at a time, but I want *all* the old highlights to be applied. @mKorbel Also, the Thread.sleep is from my code and is just used for my test code. I won't be doing that in the real app. – Mark M Jun 16 '11 at 20:55
  • @mKorbel : I didn't tried the code portion with Thread.sleep(int) so i just trusted the previous code posted. I think he/she should be careful with Thread.sleep(int) interaction with the swing GUI. – othman Jun 16 '11 at 20:55
  • @Mark M: i updated code using a loop to highlight previous charatcters backward. is that what you need to do? – othman Jun 16 '11 at 20:59
  • @othman In my real app I actually won't be highlighting all the old characters from beginning to end. There will be chunks of text that are highlighted and chunks that are not highlighted. I need to have only the old highlights be reapplied. – Mark M Jun 16 '11 at 21:06
  • @Mark M: the loop highlights only characters from "text" string so it won't highlight previous unhighlighted chunks of characters in the pane. -@mKorbel : thanks :) – othman Jun 16 '11 at 21:19