简体   繁体   English

在Java Swing JTextComponent中突出显示更改的文本

[英]Highlighting changing text in a java swing jtextcomponent

I am trying to highlight some code in a JEditorPane like this: 我正在尝试突出显示JEditorPane中的某些代码,如下所示:

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(). 我的假设是每次setText()时亮点都会消失。 So, is there any way to keep the highlights in the text component while changing the text? 因此,有什么方法可以在更改文本时将突出显示部分保留在文本组件中?

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){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM