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?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Daniel Maly
  • 317
  • 5
  • 13
  • 5
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 06 '12 at 00:08
  • It's part of a 4.5k line application but I can try... – Daniel Maly Mar 06 '12 at 00:13
  • 1
    @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? – Mike Clark Mar 06 '12 at 00:16
  • 10
    I found the problem. It was caused by setting the KEY_FRACTIONALMETRICS rendering hint to ON while painting the parent component. – Daniel Maly Mar 06 '12 at 00:31
  • 9
    +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. :) – Andrew Thompson Mar 06 '12 at 01:02
  • 4
    @DanielMaly go ahead and add a formal answer to your question and mark it as accepted. It's OK to answer your own question. – Mike Clark Mar 09 '12 at 01:14
  • 3
    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. http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Zecas May 25 '12 at 10:32

4 Answers4

1

Try this :

JTextArea textArea;
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
Abs
  • 111
  • 1
  • 2
  • 13
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;
Ravi1187342
  • 1,247
  • 7
  • 14
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());
md1hunox
  • 3,815
  • 10
  • 45
  • 67
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.

chenyi1976
  • 1,104
  • 9
  • 17