dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Applets
Expertise: Beginner
May 13, 1997

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 message and 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 create the illusion of scrolling. (I might add that I'm not terribly proud of the awkward way I compute i, j and k.)

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

The code can be modified to create a scrolling text field by passing 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 thread difficult 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;
   }   
}

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date