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

JDK 1.1, Horizontal Text Scrolling

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

Introduction

This is one 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 program was written in response to a request from a poster for code which demonstrates horizontal text scrolling.


/*File news002.java Copyright 1997, R.G.Baldwin
This program is designed to be compiled and run under JDK 1.1.
It was tested using JDK 1.1 under Win95.

This program provides a "cheap and dirty" approach to scrolling text
horizontally in a display.

The program places a TextField object in a Frame object.  A simple loop
is used along with the substring() method of the String class to 
access a portion of a String object and display it in the TextField.
The loop counter is used to cause the substring to walk across the
String object.

To avoid the complexity of wrapping around the end of the String object,
multiple copies of the same text were placed in the String object.  Thus,
the substring to be displayed can start at the beginning of the text, 
walk to the end, and then simply start over without a requirement to wrap
around the end.

The code to scroll the text was placed in a separate thread.  A call to 
sleep() was used in that thread to control the scrolling speed.  
Whenever the thread is sleeping, it relinquishes control.  This makes 
it possible for the main thread to gain control, if needed, to service 
the closing event on the Frame object to terminate the program.

Although I haven't given a lot of thought to what would be required
to convert this program back to JDK 1.0.2, on the basis of a quick scan,
it appears that the only thing that is peculiar to JDK 1.1 is the
way that I service the Frame closing event to terminate the program.
*/
//======================================================================
import java.awt.*;
import java.awt.event.*;
//======================================================================
class news002{
  static public void main(String[] args){
    GUI gui = new GUI();
  }//end main
}//end class news002
//======================================================================

class GUI{
  Frame myFrame; //container for the scrolling TextField object
  TextField myTextField; //text will scroll in this TextField object
  
  GUI(){//constructor
    myTextField = new TextField("");
    myFrame = new Frame();
    myFrame.add("South",myTextField);//place TextField in Frame
    myFrame.setSize(200,200);
    myFrame.setVisible(true);

    //Put the text scrolling code in a separate thread to make it
    // easier to terminate the program by closing the Frame object.
    Thread myThread = new Thread(new MyThread(myTextField));
    myThread.start();
    
    //Instantiate and register program termination object.
    myFrame.addWindowListener(new Terminate());    
  }//end constructor
}//end class GUI
//==========================================================
class MyThread extends Thread{
  String myString = "This is a long string.This is a long string.";
  TextField myTextField;
  MyThread(TextField textFieldIn){//constructor
    myTextField = textFieldIn;
  }//end constructor
  
  public void run(){
    while(true){
      for(int cnt = 0; cnt < 22; cnt++){
        try{
          myTextField.setText(myString.substring(cnt,cnt+20));
          try{
            Thread.currentThread().sleep(400);
          }catch(InterruptedException e){}
        }catch(StringIndexOutOfBoundsException e){}
      }//end for-loop on cnt
    }//end while-loop on true    
  }//end run
}//end class MyThread

//==========================================================
//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-