2017-08-18 7 views
0

Je crée un éditeur de texte avec la mise en évidence de la syntaxe dans Java à l'aide de JTextPane. Lorsque j'exécute le programme, j'obtiens cette sortie: https://www.dropbox.com/s/kkce9xvtriujizy/Output.JPG?dl=0La syntaxe JTextPane La mise en évidence des décalages est incorrecte

Je veux que chaque balise HTML soit surlignée en rose, mais après quelques balises elle commence à mettre en évidence les mauvaises zones.

Voici le code de mise en évidence:

private void htmlHighlight() { 
    String textToScan; 
     textToScan = txtrEdit.getText(); 
     StyledDocument doc = txtrEdit.getStyledDocument(); 
     SimpleAttributeSet sas = new SimpleAttributeSet(); 
     while(textToScan.contains(">")) { 
      StyleConstants.setForeground(sas, new Color(0xEB13B1)); 
      StyleConstants.setBold(sas, true); 
      doc.setCharacterAttributes(textToScan.indexOf('<'), textToScan.indexOf('>'), sas, false); 
      StyleConstants.setForeground(sas, Color.BLACK); 
      StyleConstants.setBold(sas, false); 
      textToScan = textToScan.substring(textToScan.indexOf('>') + 1, textToScan.length()); 
     } 

} 

Merci à l'avance!

Répondre

0

Le deuxième argument de setCharacterAttributes est la longueur, pas l'index de fin.

Cela nous donne:

private void htmlHighlight() { 
    String textToScan; 
     textToScan = txtrEdit.getText(); 
     StyledDocument doc = txtrEdit.getStyledDocument(); 
     SimpleAttributeSet sas = new SimpleAttributeSet(); 
     while(textToScan.contains(">")) { 
      StyleConstants.setForeground(sas, new Color(0xEB13B1)); 
      StyleConstants.setBold(sas, true); 
      int start = textToScan.indexOf('<'); 
      int end = textToScan.indexOf('>')+1; 
      doc.setCharacterAttributes(start, end-start, sas, false); 
      textToScan = textToScan.substring(textToScan.indexOf('>') + 1, textToScan.length()); 
     } 

} 

MISE À JOUR:

Le sous-chaîne est un problème, mais sans elle, il y a encore un décalage, peut-être en raison de la fin des lignes. La seule solution que j'ai trouvé est de recréer un nouveau document: Essayé ce,

try { 
    String textToScan; 
    textToScan = txtrEdit.getText(); 
    StyledDocument doc = new DefaultStyledDocument(); 
    SimpleAttributeSet sas = new SimpleAttributeSet(); 
    StyleConstants.setForeground(sas, new Color(0xEB13B1)); 
    StyleConstants.setBold(sas, true); 
    int end = 0; 
    while (true) { 
     int start = textToScan.indexOf('<', end); 
     if (start < 0) { 
      doc.insertString(end, textToScan.substring(end), null); 
      break; 
     } 
     doc.insertString(end, textToScan.substring(end, start), null); 
     end = textToScan.indexOf('>', start+1); 
     if (end < 0) { 
      doc.insertString(start, textToScan.substring(start), sas); 
      break; 
     } 
     ++end; 
     doc.insertString(start, textToScan.substring(start, end), sas); 
    } 
    txtrEdit.setStyledDocument(doc); 
} catch (BadLocationException ex) { 
    Logger.getLogger(MyDialog.class.getName()).log(Level.SEVERE, null, ex); 
} 
+0

mais ma nouvelle sortie ressemble maintenant à ceci: – JPadley

+0

https://www.dropbox.com/s/eb0dicxur99cr0w/newOutput.JPG ? dl = 0 – JPadley

+0

@JPadley ouais, désolé à ce sujet. Nouvelle mise à jour. –