StyledDocument doc = deinTextPane.getStyledDocument();
SimpleAttributeSet color = new SimpleAttributeSet();
StyleConstants.setForeground(color, Color.RED);
doc.setCharacterAttributes(start, end, color, true);
public void highlight(JTextPane textPane) {
StyledDocument doc = textPane.getStyledDocument();
boolean hl = false;
String text = textPane.getText();
for(int i = 0; i < text.length(); i++)
{
if(text.substring(i,i+1).equals("<"))
hl = true;
if(hl)
doc.setCharacterAttributes(i,1,color,true);
if(text.substring(i,i+1).equals(">"))
hl = false;
}
}
Das Problem sieht folgendermaßen aus:Hobbit_Im_Blutrausch hat gesagt.:Warum sollte substring Probleme mit Zeilenumbrüchen haben?
public void htmlText(JTextPane pane)
{
StyledDocument pDoc = pane.getStyledDocument();
String text = pane.getText();
text = text.replaceAll("\r","");
char[] array = new char[text.length()];
text.getChars(0,text.length(),array,0);
int level = 0;
for(int i = 0; i < text.length(); i++)
{
char c = array[i];
switch(level)
{
// Normaler Text
case 0:
pDoc.setCharacterAttributes(i, i, normal, true);
if(c == '<')
{
if(array[i+1] == '!' && array[i+2] == '-' && array[i+3] == '-')
{
level = 4;
pDoc.setCharacterAttributes(i, i, grey, true);
break;
}
level = 1;
}
break;
// Innerhalb eines Tags
case 1:
if(c == '>')
{
level = 0;
pDoc.setCharacterAttributes(i, i, normal, true);
break;
}
if(c == ' ')
{
level = 2;
break;
}
pDoc.setCharacterAttributes(i, i, red, true);
break;
// Attribute des Tags
case 2:
if(c == '>')
{
pDoc.setCharacterAttributes(i, i, normal, true);
level = 0;
break;
}
if(c == '"')
{
level = 3;
pDoc.setCharacterAttributes(i, i, blue, true);
break;
}
pDoc.setCharacterAttributes(i, i, black, true);
break;
// Werte der Attribute in "..."
case 3:
pDoc.setCharacterAttributes(i, i, blue, true);
if(c == '"')
{
level = 2;
}
break;
// Kommentare
case 4:
pDoc.setCharacterAttributes(i, i, grey, true);
if(c == '-' && array[i+1] == '-' && array[i+2] == '>')
{
pDoc.setCharacterAttributes(i+1, i+1, grey, true);
pDoc.setCharacterAttributes(i+2, i+2, grey, true);
i+=3;
level = 0;
}
break;
}
}
}