Aligning Textarea


OK, this is probably really simple for a seasoned Java coder, but, I have
an application that opens a textarea as a log window. However, whenever
the window fills, the window does not automatically scroll down. Is there
a way to this?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

try following example :
simply click on applet field to add row ro TextArea
This one scrolls in appletviewer in JDK1.02
I think you do not appended text but replaced all text setting
it with setText() method. That's why it doesn't scroll

import java.awt.*;
import java.applet.*;

        public class check extends Applet
        {int i=0;
         TextArea ta;
         public void init()
         {ta=new TextArea(10,10);
         public boolean mouseDown(Event e, int x, int y)
         { ta.appendText("\n"+i);
           return true;

slyfoxxAuthor Commented:
Sorry, No good.

Irrespective of whether I append to the textarea or recreate all the text, the view of the text in the textarea is the same, the view does not scroll down automatically. (i.e. Functionally the same as the scroll bar being at the top and not moving.)

I am running jdk 1.1.3 on a solaris 2.5 machine under X.

Here is a copy of the code I am using.

import java.awt.*;

public class monitor {
  Frame fr = new Frame("Simulator Monitor");
  TextArea txt = new TextArea("", 12, 72);
  String text = "";

  monitor() {
    fr.setFont(new Font("Courier", Font.BOLD, 10));
    txt.append("Socket Connection Established\n");

 public void title(String t) {
    fr.setTitle("MR Simulator Monitor: "+t);

  public void add(String t) {
    text = text+t;

  public void add(int c, String t) {
    text = text+("("+c+") "+t);

  public void addln(String t) {
    text = text+t+"\n";

  public void addln(int c, String t) {
    text = text+"("+c+") "+t+"\n";

  public void addln() {
    text = text+"\n";

  public void update() {
    int end = text.length();
    txt.replaceRange(text, 0, end);

I'd recommend using a List instead of a TextArea.

When you add an item call list.size()-1 );

to select the last item in this list.  This will scroll the list
so that the last line is selected.  The downside, is that
List doesn't come with a horizontal scrollbar, so each item
should fit within the width of your control.

Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slyfoxxAuthor Commented:
Interesting, I hadn't thought of it that way... Thanks...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.