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'):
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?
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.
Constructs a new TextArea. A default model is set, the initial string is null, and rows/columns are set to 0.
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.
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.
Try this :
JTextArea textArea;
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With