FileDocCategorySizeDatePackage
Log.javaAPI DocExample1060Tue Nov 13 16:41:20 GMT 2001None

Log.java

// File: Log.java
// T Balls : Nov 2001
// Class to provide a scrolling text pane

import javax.swing.*;
import java.awt.*;

/** 
 * Encapsulate a scrolling text area that ensures that new text,
 * added at thebottom of the area is always visible on screen
 */
public class Log extends JPanel
{
   public Log( int rows, int columns )
   {
      text = new JTextArea( rows, columns );
      text.setEditable( false );

      setLayout( new BorderLayout() );
      add( new JScrollPane( text, 
                            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ),
           BorderLayout.CENTER );
   }

   private JTextArea text;
  /**
    * Append String value to end of log
    *
    * Scroll area if necessary so that new text is visible.
    * Scrolling is achieved by ensuring that the text caret is
    * positioned at the end of the buffer.
    */
   public void append( String message )
   {
      text.append( message );
      text.setCaretPosition( text.getText().length() );
   }
}