die Methoden getLineStartOffset, getLineEndOffset und getLineOfOffset der Klasse JTextArea nachzubauen?! :x :cry:
Ich versuch das jetzt schon seit mind. 4 Tagen, und es funzt immer noch nicht ganz!!!
Warum hat Sun die eigentlich nicht auch in die String-Klasse geschrieben?
Mein Code:
Ich hab leider im Mom keine Ahnung wo genau der/die Fehler jetzt liegen!
Ich waeis nur das im Moment:
Eine BadLocationException in der letzten Zeile auslöst.
Währe sehr dankbar wenn ihr mir helfen könntet! Langsam geht mit das Problem auf den ***! *Verzweifel*
Michi
Ich versuch das jetzt schon seit mind. 4 Tagen, und es funzt immer noch nicht ganz!!!
Warum hat Sun die eigentlich nicht auch in die String-Klasse geschrieben?
Mein Code:
Code:
private int getLineStartOffset(String text, int line) throws javax.swing.text.BadLocationException
{
if(text == null)
throw new IllegalArgumentException ("Arrgument 'null' is not allowed in getLineOfOffset(String text, int off)");
int offset = 0;
for(int l=1; l<line; l++)
{
int o1 = text.indexOf("\r", offset);
int o2 = text.indexOf("\n", offset);
if(o1 == -1 && o2 == -1)
{
throw new javax.swing.text.BadLocationException("String dosn't contain so many lines: ", line);
}
if(o1+1==o2)
offset = o2+1;
else if(o1 == -1)
offset = o2+1;
else if(o2 == -1)
offset = o1+1;
else
offset = Math.min(o1+1, o2)+1;
}
return offset;
}
private int getLineEndOffset(String text, int line) throws javax.swing.text.BadLocationException
{
if(text == null)
throw new IllegalArgumentException ("Arrgument 'null' is not allowed in getLineOfOffset(String text, int off)");
int position = 0;
int offset = 0;
for(int l=1; l<=line; l++)
{
int o1 = text.indexOf("\r", position);
int o2 = text.indexOf("\n", position);
if(o1 == -1 && o2 == -1)
{
if(l==line)
{
offset = text.length();
break;
}
else
throw new javax.swing.text.BadLocationException("String dosn't contain so many lines: ", line);
}
position = Math.max(o1, o2) +1;
if(o1==o2-1)
offset = o1-1;
else if(o1 == -1)
offset = o2-1;
else if(o2 == -1)
offset = o1-1;
else
offset = Math.min(o1, o2)-1;
}
return offset;
}
private int getLineOfOffset(String text, int off) throws javax.swing.text.BadLocationException
{
if(text == null)
throw new IllegalArgumentException ("Arrgument 'null' is not allowed in getLineOfOffset(String text, int off)");
if(text.length()<off)
throw new javax.swing.text.BadLocationException("String dosn't contain so many offsets: " , off);
if(off<0)
throw new javax.swing.text.BadLocationException("Offsets must be greater then zero: " , off);
int line = 0;
for(int o = 0; o<off;)
{
int o1 = text.indexOf("\r", o+1);
int o2 = text.indexOf("\n", o+1);
if(o1 == -1 && o2 == -1)
{
line++;
break;
}
if(off==o1)
{
line++;
break;
}
else if(off==o2)
{
line += 2;
break;
}
if(o1==o2-1)
o = o2;
else if(o1 == -1)
o = o2;
else if(o2 == -1)
o = o1;
else
o = Math.min(o1, o2);
line++;
}
return line;
}
Ich hab leider im Mom keine Ahnung wo genau der/die Fehler jetzt liegen!
Ich waeis nur das im Moment:
Code:
zeilen = getLineOfOffset(text, to ) - getLineOfOffset(text, from ) + 1;
i= zeile;
System.out.println("s: " + getLineStartOffset(text, i));
System.out.println("e: " + getLineEndOffset(text, i));
Währe sehr dankbar wenn ihr mir helfen könntet! Langsam geht mit das Problem auf den ***! *Verzweifel*
Michi