3 Replies Latest reply on Feb 13, 2006 10:36 AM by 843805

    Content-Overlay in JTextPane

    843805
      Hi all,

      I use a JTextPane within a JScrollPane. The JTextPane uses it' own Syntax-Highlighter.
      Sometimes appears an overlay of text. you enter some text and suddenly the new line ist written over the previous line ???
      Often there isn't much content in the TextPane an the computer is "state of the art" 2 GB RAM with an 3,4 GHz Processor. I think it isn'a performance-problem.

      Has anyone an advice for me ???

      Thanks,
      Markus
        • 1. Re: Content-Overlay in JTextPane
          843805
          ... I use the latest update of Java 1.4.2

          Bye!
          • 2. Re: Content-Overlay in JTextPane
            camickr
            The JTextPane uses it' own Syntax-Highlighter.
            Think there might be a problem with the Syntax-Highlighter??
            • 3. Re: Content-Overlay in JTextPane
              843805
              Hi,

              for example I post the Highlighter-Code:
              import java.awt.Color;
              import java.util.regex.Matcher;
              import java.util.regex.Pattern;
              
              import javax.swing.JPanel;
              import javax.swing.JTextPane;
              import javax.swing.event.DocumentEvent;
              import javax.swing.event.DocumentListener;
              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;
              import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;
              import javax.swing.undo.UndoManager;
              
              
              /**
               * @author
               *
               */
              public class ExtendedTextPane extends JTextPane {
              
                  private JPanel editor;
                  private Highlighter highlighter;
                  private WordSearcher searcher   = new WordSearcher(this);
                  
                  private DocumentListener docuListener = new DocumentListener() {
                      public void insertUpdate(DocumentEvent evt) {
                          searcher.searchRegex();
                        }
                  
                        public void removeUpdate(DocumentEvent evt) {
                          searcher.searchRegex();
                        }
                  
                        public void changedUpdate(DocumentEvent evt) {
                        }
                  };
                  
                  private boolean has2Coloring = true;
                  private boolean decision = false;
              
                  
                  
                  /**
                   * Constructor
                   * @param editor - Referenz to an EditorPanel
                   */
                  public ExtendedTextPane( int column, UndoManager undoManager, JPanel editor ) {
                      this.editor = editor;
                      this.highlighter = new CodeHighlighter();
                      this.setHighlighter(highlighter);
                      this.getDocument().addDocumentListener(docuListener);
                      this.getDocument().addUndoableEditListener(undoManager);
              
                  }
                  
                  
                  /**
                   * Own Highlighter to highlight some expressions 
                   */
                  class CodeHighlighter extends DefaultHighlighter {
                      
                      public static final int CODE1  = 0;
                      public static final int CODE2  = 1; 
                      private Color code1Color      = new Color(224,224,224); // grey
                      private Color code1ColorOther = new Color(166,210,255); // blue
                      private Color code2Color      = new Color(240,228,159); // yellow
                            
                      // Painter for highlighting of Code1
                      protected Highlighter.HighlightPainter codePainter;
                      
                      // Painter for highlighting of Code2
                      protected Highlighter.HighlightPainter textPainter;
              
                      
                      /**
                       */
                      public CodeHighlighter() {         
                          this.code1Color  = decision ? code1ColorOther : code1Color;
                          codePainter  = new CodeHighlightPainter(code1Color);
                          textPainter  = new TextHighlightPainter(code2Color);
                      }
              
                      /**
                       * Set the painter that I need
                       * @param type
                       * @return the individual Painter
                       */
                      public Highlighter.HighlightPainter getPainter(int type) {
                          Highlighter.HighlightPainter painter = null;
                          
                          switch (type) {
                              case 0: painter = codePainter;
                              break;
                              
                              case 1: painter = textPainter;
                              break;
                          }            
                          return painter;
                      }
              
                      // Painter for CodeHighlights
                      public class CodeHighlightPainter extends
                      DefaultHighlightPainter {
                          public CodeHighlightPainter(Color c) {
                              super(c);
                          }
                      }
                      
                      // Painter for TextHighlights
                      public class TextHighlightPainter extends
                      DefaultHighlightPainter {
                          public TextHighlightPainter(Color c) {
                              super(c);
                          }
                      } 
                  }
                  
                  
                  
                  /**
                   * Class highlights some text, that matches specific regular expressions
                   *
                   */    
                  class WordSearcher {
                      protected JTextComponent comp;
                      protected Highlighter.HighlightPainter painter;
                      // Regul�re Ausdr�cke
                      private Pattern code     = Pattern.compile("expression1");
                      private Pattern textKeys = Pattern.compile("expression2");
                    
                      /**
                       * Constructor
                       */
                      public WordSearcher(JTextComponent comp) {    
                          this.comp = comp;
                      }
              
                      /**
                       * Search through the complete Document for matching expressions
                       */ 
                      public void searchRegex() {  
                          Highlighter highlighter = comp.getHighlighter();
                          if(highlighter == null || !has2Coloring)
                              return;
                          // delete existing Highlights of the last expression
                          Highlighter.Highlight[] highlights = highlighter.getHighlights();       
                          for (int i = 0; i < highlights.length; i++) {
                            Highlighter.Highlight h = highlights;
              if (h.getPainter() instanceof CodeHighlighter.CodeHighlightPainter ||
              h.getPainter() instanceof CodeHighlighter.TextHighlightPainter ) {
              highlighter.removeHighlight(h);
              }
              }

              String content = null;
              try {
              Document d = comp.getDocument();
              content = d.getText(0, d.getLength());
              } catch (BadLocationException e) {
              e.printStackTrace();
              }

              int start = 0;
              int lastIndex = 0;
              Matcher m = code.matcher( content );

              // Search for Code-Expressions
              while ( m.find(start) ) {
              try {
              highlighter.addHighlight(m.start(), m.start() + m.group().length(), ((CodeHighlighter) highlighter).getPainter(CodeHighlighter.CODE1));
              } catch (BadLocationException e) {
              e.printStackTrace();
              }
              start = m.end();
              lastIndex = start;
              }

              start = 0;
              m = textKeys.matcher( content );
              // Search for Text-Expressions
              while ( m.find(start) ) {
              try {
              highlighter.addHighlight(m.start(), m.start() + m.group().length(), ((CodeHighlighter) highlighter).getPainter(CodeHighlighter.CODE2));
              } catch (BadLocationException e) {
              e.printStackTrace();
              }
              start = m.end();
              lastIndex = start;
              }
              }
              }
              }


              How does it work ?
              A JPanel-Component creates an instance of this class.
              The Component works as a reference in the ExtendedTextPane.
              the <editor> now calls setText() or getText(). The component highlights certain scopes.
              Maybe someone finds a bug. But again: the problem appears rarely