Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Aligning Textarea

Posted on 1997-08-21
4
Medium Priority
?
489 Views
Last Modified: 2012-05-04
Question...

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?

Thanks...
0
Comment
Question by:slyfoxx
  • 2
4 Comments
 
LVL 10

Expert Comment

by:kollegov
ID: 1225514
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.net.*;
import java.applet.*;

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

        }
0
 

Author Comment

by:slyfoxx
ID: 1225515
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.setSize(400,400);
    fr.setFont(new Font("Courier", Font.BOLD, 10));
    txt.setEditable(false);
    fr.add(txt);
    txt.append("----------------------------------\n");
    txt.append("Socket Connection Established\n");
    txt.append("----------------------------------\n\n");
    fr.show();
    fr.setLocation(300,300);
    }

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

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

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

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

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

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

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

}
0
 
LVL 1

Accepted Solution

by:
gerry99 earned 100 total points
ID: 1225516
I'd recommend using a List instead of a TextArea.

When you add an item call  

list.select( 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.
0
 

Author Comment

by:slyfoxx
ID: 1225517
Interesting, I hadn't thought of it that way... Thanks...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question