Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caret position is off in JTextArea

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?

like image 226
Daniel Maly Avatar asked Mar 05 '12 23:03

Daniel Maly


People also ask

What is caret Swing Java?

A place within a document view that represents where things can be inserted into the document model. A caret has a position in the document referred to as a dot. The dot is where the caret is currently located in the model.

What is the default found for JTextArea?

Constructs a new TextArea. A default model is set, the initial string is null, and rows/columns are set to 0.

How do I disable JTextArea?

If you would like to just disable text selection on any swing control such as JtextArea you can use the coding below: JtextArea. setHighlighter(null); This one line of coding will help disable the text selection and can be placed in the constructor or within a initialized method upon Frame execution.

What is JTextArea in Java?

A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java. awt. TextArea class where it can reasonably do so.


1 Answers

Try this :

JTextArea textArea;
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
like image 87
Abs Avatar answered Oct 02 '22 12:10

Abs