Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

JDK 1.1, Controlling the Visible Text in a Text Area on Startup

Java Programming, Lecture Notes # 900, Revised 3/20/97.

Introduction

This is the first in a planned series of documents which will be written in response to questions posted on the various Java newsgroups.

In most cases, the response will be in the form of a sample program. In most cases, the programs will be written using JDK 1.1 unless there is some special reason to backtrack to JDK 1.0.2.

Also, in most cases, there will be very little discussion other than the comments contained in the program.

Sample Program

This section contains a sample program designed to answer some or all of the questions raised in the posting.


/*File news001.java Copyright 1997, R.G.Baldwin
Designed to be compiled and run under JDK 1.1.
Written and tested using JDK 1.1 under Win95.

This program was written in response to a newsgroup poster who
was having difficulty controlling the initially visible text in
a TextArea object.

The program places a non-editible TextArea object in a Frame
object and then writes 99 lines of text into the TextArea object.

The setCaret() method of the TextComponent class is used to establish
the portion of the text that is visible on startup.

After the Frame appears on the screen, the vertical scroll bar can 
be used to scroll up and down in the text.

Note that the default appears to be to display the beginning of the 
text on startup.
*/

import java.awt.*;
import java.awt.event.*;
//===================================================================
class news001{
  public static void main(String[] args){
    GUI gui = new GUI(); //instantiate a user interface object
  }//end main()
}//end class001
//===================================================================
class GUI{
  GUI(){//constructor
    Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin");
    TextArea myTextArea = new TextArea("Text Line 0\n");
    myTextArea.setEditable(false);
    
    for(int cnt = 1; cnt <100; cnt++)//add 98 more lines of text
      myTextArea.append("Text Line " + cnt + "");
    myFrame.add("Center",myTextArea);//place test area in the Frame

    myFrame.setSize(300,300);
    myFrame.setVisible(true);
    //The following statement uses the character count to establish
    // that portion of the text in the TextArea object that is
    // visible on startup. This statement causes character number
    // 1200 to be visible.  
    //To see the default case where the beginning of the text is
    // visible on startup, comment the following statement out 
    // and recompile.
    //Note that in order to avoid a "peer" runtime error, this 
    // statement must not execute until the Frame object is visible.
    myTextArea.setCaretPosition(1200);//must follow setVisible()         

    //Instantiate and register program termination object.
    myFrame.addWindowListener(new Terminate());    
  }//end constructor
}//end class GUI
//==========================================================
//Terminates program when the user closes the Frame object.
class Terminate extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }//end windowClosing()
}//end class Terminate
//==========================================================
-end-