devxlogo

Scrolling status bar message

Scrolling status bar message

Question:
How can I make a message that will scroll in the status bar?

Answer:
The following applet constructs a thread, passing it a messageand its browser context. The thread perpetually sleeps for 100 miliseconds and computes three strings:

 prefix = last i chars of message;   infix  = first j chars of filler;  // filler = ”     ”   suffix = first k chars of message;
or
prefix = last j chars of filler;   infix  = first k chars of message;     suffix = first i chars of filler;
where i + j + k = blen = fixed banner length, then calls:
 context.showStatus(prefix + infix + suffix);
Each time through the loop, i, j and k are adjusted to createthe illusion of scrolling. (I might add that I’m not terriblyproud of the awkward way I compute i, j and k.)

From the applet, users can suspend or resume the thread, orchange the message.

The code can be modified to create a scrolling text field bypassing the text field rather than a context to the thread.In this case we use the statement:

msgField.setText(prefix + infix + suffix);
Finally, the 100 milisecond figure was arrived at experimentally.Shorter times make the text scroll too fast and make the threaddifficult to interrupt.
*/import java.awt.*;import java.applet.*;class Scroller extends Thread {   AppletContext context;   String msg;   String filler = ”          “;   public Scroller(String m, AppletContext ctxt) {      context = ctxt;      msg = m;   }   public void run() {      int mlen = msg.length(),          flen = filler.length(),          blen = mlen + flen – 2,          i = 0,          j = 0,          k = 0,          h = 0;      String prefix, infix, suffix;      while (true) {         if (i < mlen) {            prefix = msg.substring(i, mlen);            h = mlen - i;            infix = filler.substring(0, j = Math.min(blen - h, flen));            if (blen - (h + j) >= 0)               suffix = msg.substring(0, Math.min(blen – (h + j), mlen));            else               suffix = “”;         }         else {            prefix = filler.substring(j = (i – mlen), flen);            h = flen – j;            infix = msg.substring(0, k = Math.min(blen – h, mlen));            if (blen – (h + k) >= 0)               suffix = filler.substring(0, Math.min(blen – (h + k), flen));            else               suffix = “”;         }         i = (i + 1) % (flen + mlen);                  try {            sleep(100);         }         catch (InterruptedException e) {            e.printStackTrace();         }         context.showStatus(prefix + infix + suffix);      } // while   } // run}         public class BannerDemo extends Applet {   private AppletContext context;   private String message;   private Scroller scrollGuy;   private boolean scrolling = true, msgChanged = false;   private TextField msgField;   public void init() {      add(new Button(“Suspend/Resume”));      context = getAppletContext();      message = getParameter(“msg”);      msgField = new TextField(message, 20);      add(msgField);      scrollGuy = new Scroller(message, context);   }   public void start() {      scrollGuy.start();      scrolling = true;   }   public void stop() {      scrollGuy.stop();      scrolling = false;   }   public boolean handleEvent(Event e) {      if (e.id == Event.WINDOW_DESTROY) System.exit(1);      return super.handleEvent(e);   }   public boolean action(Event e, Object arg) {      if (arg.equals(“Suspend/Resume”) ) {         if (scrolling) {            scrollGuy.suspend();            scrolling = false;         }         else if (msgChanged) {            scrolling = true;            msgChanged = false;            scrollGuy.stop();            scrollGuy = new Scroller(msgField.getText(), context);            scrollGuy.start();         }         else {            scrollGuy.resume();            scrolling = true;         }      }      else if (e.target == msgField)         msgChanged = true;      else return false;      return true;   }   }

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist