Share this page 

Have a srolling text displayTag(s): AWT


A srolling text display can be used to log informations or events. In this snippet, we are using a List component to display the data. We are keeping only the last 10 events. It's a good idea to limit the amount of data kept in the List , in fact we have no choice because the maximum capacity is about 32k (Win).
import java.awt.*;
import java.awt.event.*;
 
 public class TelnetLikeDisplay extends Applet 
    implements ActionListener {
   Button b;
   int i = 0;
   static final int LINE_BUFFERED = 10;
   static final int LINE_DISPLAYED = LINE_BUFFERED - 1;
   List l;
   
   public void init() {
     setLayout(new FlowLayout(FlowLayout.LEFT));    
     add(b = new Button("New Line"));    
     b.addActionListener(this);
     add(l = new List(5));
     l.setSize(100,100);
     l.setForeground(new Color(0).yellow);
     l.setBackground(new Color(0).black);
     }

   public void actionPerformed(ActionEvent ae) {
     String newLine = "Line #" + i;
     if (i < LINE_BUFFERED) {
        l.addItem(newLine);
        l.makeVisible(i);
        }
     else {
        l.remove(0);
        l.add(newLine, LINE_DISPLAYED);
        l.makeVisible(LINE_DISPLAYED);
        }
     i++;
     }
}