M Heymann
M Heymann

Reputation: 1

jtextpane cursor at end after new line is different size

My users found that if the last character in a JTextPane is a newline, the cursor is smaller. Debug statements showed the same cursor and the same font no matter where I clicked. I downloaded the Oracle demo for JTextPane and can see the same behaviour, so it appears to be out of the box behaviour.

Anyone know a way around this?

Upvotes: 0

Views: 191

Answers (1)

camickr
camickr

Reputation: 324118

My users found that if the last character in a JTextPane is a newline, the cursor is smaller

The caret represents the height of the largest Font used on the line. Since there is no text a smaller caret is used. Is this really an issue to worry about?

I downloaded the Oracle demo for JTextPane and can see the same behaviour

Add the following line of code to the TextComponentDemo:

Rectangle caretCoords = textPane.modelToView(dot);
System.out.println(caretCoords); // added

You will see that the height changes. The height of the Rectangle is used by the DefaultCaret class to paint the caret.

I guess you could override the modelToView(...) method of JTextPane to return a minimum height based on the FontMetrics of the Font of the text pane. Not sure what other functionality of the text pane that might affect.

Or you could override the paint() method of the DefaultCaret to use a minimum height, again based on the FontMetrics.

Upvotes: 2

Related Questions