JEditorPane with variable line height
Hi folks,
I'm trying to build a custom editor using the HTMLEditorKit. I want the user to be able to change font size and style.
How do I get the current line number the cursor is placed? That would be no problem if I used constant line height. But I don't.
I tried some stuff with getCaretPosition() and the other stuff I found in the forums but with no satisfying result.
Any ideas?
Thnx in advance,
AvonObi
[457 byte] By [
AvonObia] at [2007-11-27 8:52:37]

# 1
/*
** Return the current line number at the Caret position.
*/
public static int getLineAtCaret(JTextComponent component)
{
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
return root.getElementIndex( caretPosition ) + 1;
}
# 3
If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",
see http://homepage1.nifty.com/algafield/sscce.html,
that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the "Code Formatting Tags",
see http://forum.java.sun.com/help.jspa?sec=formatting,
so the posted code retains its original formatting.
# 4
Thanx for that one.
But I found a different solution which also got rid of another problem I had.
I used the follow thread as advice:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=649401
Just use a StringWriter and write your document into it. After that throw away all final closing tags and count the number of <div> tags in your writer (These are created automatically).
Tata, the current line number.
Cheers.