Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Prevent scrolling when appending to JTextArea  RSS feed

 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I have two JTextAreas side by side in an application. One of the JTextAreas is for editing text, and the narrow one to the side of it is for line numbering. I am listening for a variety of events in my main editable JTextArea so that when the user types something, the line count is checked and the line numbering text area is updated accordingly (e.g. if the user pastes new lines in or hits enter, thus increasing the line count). I am using the append() method to add new line numbers to the line numbering text area.
My problem is that when append() is called, the text area is automatically scrolled down to the last line and therefore so is the editing text area (both text areas are in the same scroll pane). So the position of the caret goes out of view whenever you type anything or cut and paste. I would be grateful of suggestions as to how to prevent my text areas from scrolling to the bottom when they are appended to, or, alternatively, does anyone have a better way to do line numbering?
cheers,
Ben.
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
Funny coincidence; I'm working on a small app where I want the opposite as you---for the scroll pane to scroll down to the bottom every time text gets added to the text area. I'm also using append(). It's not quite working right for me yet... Here's my code for appending stuff to the JTextArea:

It doesn't set the scroll bar all the way to the bottom; something fishy is going on. Oh well, maybe my problem will shed some light on yours.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for the reply, John. I think my app is working a little differently to yours in that I am using a JScrollPane, rather than scrollbars. The behaviour of the scrollpane seems such that it auto-scrolls to the end of the text area when append() is called - which would seem logical in certain circumstances, although predictably not in mine! Perhaps it would be a good idea for me to set up my own scrollbars instead, like you. I'm fairly new to swing really, which is why i am using scrollpane as it seems easier to implement. Another way round the problem that I thought of is to use insert() instead, which doesn't cause my textarea to scroll when called, but that could get messy!
regards, Ben.
 
Julie Katz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using JEditorPane. It will not scroll to the bottom automatically as JTextArea does (annoyingly).
Here's the code (adapted from Flanagan's *JFC in a Nutshell*/O'Reilly)
<pre>
JEditorPane myText = new JEditorPane();
...
try {
myText.setContentType("text/plain");
InputStream in = new FileInputStream(myTextFile);
myText.read(in, null);
}
catch (IOException e) { ... whatever ... }
</pre>
Then, of course, add myText to your JScrollPane. You can get some really spiffy output by using "text/html" or "text/rtf." Another cool thing is that JEditorPane can also be made editable.
 
Jeff Chang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
yourJTextArea.select(1, 1);
It should work because it works well for my project.
Good luck! :-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!