Caret position is off in JTextArea
Asked Answered
S

4

13

I am adding a JTextArea to a component with a layout manager that respects preferred size and location. However, the position of the displayed caret in the JTextArea becomes erroneous after typing a few "wide" letters (e.g. 'm'):

enter image description here

This is after having typed all the letters from the left and the actual caret position is after 'd'. The JTextArea in this case is much wider than the text. Not sure if it might be relevant but the font used is Arial, size 11, plain style and is being set before adding the JTextArea to the parent container. Any ideas what might be causing this?

Stroboscope answered 5/3, 2012 at 23:58 Comment(7)
For better help sooner, post an SSCCE.Musk
It's part of a 4.5k line application but I can try...Stroboscope
@DanielMaly - I agree with Andrew. I've done quite a bit of Swing work, and nothing about this problem jumps out at me as having an obvious solution. Isolate the problem to a simple program so we can try it out. If you can't post a SSCCE, then at least tell us what LayoutManagers specifically are you using? A custom one?Dix
I found the problem. It was caused by setting the KEY_FRACTIONALMETRICS rendering hint to ON while painting the parent component.Stroboscope
+1 for reporting back the source of the problem. Perhaps you should enter that as an answer & mark it correct (when the site lets you - from memory it is not immediate). Glad you got it sorted. :)Musk
@DanielMaly go ahead and add a formal answer to your question and mark it as accepted. It's OK to answer your own question.Dix
Just a friendly reminder @DanielMaly. Both Andrew and Mike are right. StackOverFlow encourages as standard procedure that you answer yourself. Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. blog.stackoverflow.com/2011/07/…Workhorse
P
1

Check this out. It works.

I have used this in one of my applications.

 Rectangle r = textArea.modelToView( textArea.getCaretPosition() );
 int  caretX = r.x;
 int  caretY = r.y;
Pickings answered 29/3, 2012 at 14:35 Comment(0)
E
1

Try this :

JTextArea textArea;
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
Elbow answered 3/8, 2012 at 19:55 Comment(0)
R
0

I actually meet same issue when using JBuilder before, the caret position is not in the position they should be.

It only happened for Windows chinese version, if the system is english version, it is fine..

To solve it, just goes to jbuilder setting, change all fonts to a chinese font.

It is not jbuilder issue, it happens to other java application too, actually it is a JRE issue, goes to JRE/lib directory, modify font.properties or fontconfig.peroperties.src (depends on different jre version), add the font there will solve this issue.

I guess this is not the answer to your question, but maybe happen someone, just for your reference.

Rookie answered 19/3, 2012 at 6:57 Comment(0)
E
0

you can also try to forcibly place caret at the end of the text using something like the folloiwng:

JTextArea displayArea;    
displayArea.setCaretPosition(displayArea.getDocument().getLength());
Emphatic answered 12/6, 2012 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.