?
Solved

Scrolling text horizontally in a JLabel within a Java app

Posted on 2006-03-23
14
Medium Priority
?
562 Views
Last Modified: 2008-02-01
Hello,

I am after some information on how the above is possible, I did use a timer but caused major issues with screen updating, would like to know if this can be done using threads as I did try a Swingworker class but this caused the same issue. Any information would be much appreciated.

cheers

Cyart
0
Comment
Question by:Cyart
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16267511
You need a *SWing* Timer
0
 
LVL 37

Expert Comment

by:zzynx
ID: 16267531
This can surely inspire you:

CreditsScroller.java
------------------------

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

public class CreditsScroller extends JPanel implements Runnable {

     private boolean stop = false;
     String[] headlines = {

     " This program was made by Computer Wizard :", "",
     "                Cyart           ", };

     int y = 76;
     
     public void stopIt() {
         stop = true;
     }

     public void run() {

          while (!stop) {
              y = y - 1;

              if (y < -650)
              y = 76;

              repaint();

              try {
              Thread.sleep(30);
              } catch (InterruptedException e) {
              }
          }
     }

     public void paintComponent(Graphics comp) {

          Graphics2D comp2D = (Graphics2D) comp;
          Font type = new Font("monospaced", Font.BOLD, 14);
          comp2D.setFont(type);
          comp2D.setBackground(Color.black);
          comp2D.fillRect(0, 0, getSize().width, getSize().height);
          comp2D.setColor(Color.white);
          for (int i = 0; i < headlines.length; i++)
            comp2D.drawString(headlines[i], 5, y + (20 * i));
     }
}

CreditScrollDemo.java
----------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CreditScrollDemo extends JFrame {

     CreditsScroller news = new CreditsScroller();
     private Thread scroller = new Thread(news);

     public CreditScrollDemo() {
         super("Authors . . .");
         setSize(400, 300);
         JPanel pane = new JPanel();
         pane.setLayout(new GridLayout(1, 1, 15, 15));
         pane.add(news);
         setContentPane(pane);
         setVisible(true);
         scroller.start();
     }
     
     public void stopScrollThread() {
         news.stopIt();
     }
     
     public static void main(String[] arguments) {
        final CreditScrollDemo app = new CreditScrollDemo();
        app.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                 app.stopScrollThread();
             }
        });
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
}  
0
 
LVL 92

Assisted Solution

by:objects
objects earned 800 total points
ID: 16267562
you wouldn't use a JLabel, smoothest can be acieved by painting to an offscreen image and displaying that image.
and you don't need a thread or swing worker either, a timer is all u need.
there are 1000's of these available on the net, just do a search for a ticker
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Cyart
ID: 16268100
Just been trying to find ticker examples and it all seems to point to applets.

zzynx, I will look at the code later as I have not got java installed on this PC
0
 
LVL 92

Expert Comment

by:objects
ID: 16268129
> Just been trying to find ticker examples and it all seems to point to applets.

applets are no different than applications for what you want to do, they just run in a browser.
The code to do the ticker can also be used in an application.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 16268373
>> zzynx, I will look at the code later as I have not got java installed on this PC
That's OK
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 16268502
Try something like this:


class ScrollingBannerLabel extends JLabel implements ActionListener {
      private Image offscreenImage;

      private Timer timer;

      private int delay;

      private int deltaX;

      private int x, y;

      public ScrollingBannerLabel(String text) {
            super(text);
            deltaX = 1;
            delay = 50;
      }

      /*
       * (non-Javadoc)
       *
       * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
       */
      protected void paintComponent(Graphics g) {
            if (offscreenImage == null) {
                  offscreenImage = new BufferedImage(getWidth(), getHeight(),
                              BufferedImage.TYPE_INT_RGB);
                  Graphics imageGraphics = offscreenImage.getGraphics();
                  imageGraphics.setColor(Color.white);
                  imageGraphics.fillRect(0, 0, offscreenImage.getWidth(this),
                              offscreenImage.getHeight(this));
                  super.paintComponent(imageGraphics);
            }
            g.drawImage(offscreenImage, x, y, this);

      }

      public void startScroll() {
            if (timer == null) {
                  timer = new Timer(delay, this);
            }
            timer.start();
      }

      public void stopScroll() {
            if (timer != null) {
                  timer.stop();
            }
      }

      public void actionPerformed(ActionEvent e) {
            x += deltaX;
            int width = getWidth();
            if (x > width) {
                  x = -width;
            }
            repaint();
      }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 16275408
Looks like CEHJ agreed with my suggestions , and saved me some typing :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16275545
>>Looks like CEHJ agreed with my suggestions

No ;-)

>>you wouldn't use a JLabel,

>>ScrollingBannerLabel extends JLabel
0
 
LVL 92

Expert Comment

by:objects
ID: 16275610
Didn't say your implementation was perfect, it includes a few other little *issues* as well :)
The type of component is actually irrelevant, using a JLabel will work it just carries extra innecessary baggage that u don't need.
You could equally use a JPanel or even a JComponent.
0
 

Author Comment

by:Cyart
ID: 16282333
Thanks for the info

How does this code work so that it wraps the text around the GUI

 /*
      * (non-Javadoc)
      *
      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
      */
     protected void paintComponent(Graphics g) {
          if (offscreenImage == null) {
               offscreenImage = new BufferedImage(getWidth(), getHeight(),
                         BufferedImage.TYPE_INT_RGB);
               Graphics imageGraphics = offscreenImage.getGraphics();
               imageGraphics.setColor(Color.white);
               imageGraphics.fillRect(0, 0, offscreenImage.getWidth(this),
                         offscreenImage.getHeight(this));
               super.paintComponent(imageGraphics);
          }
          g.drawImage(offscreenImage, x, y, this);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16283040
The value of 'x' increments on firing of the Timer. If it gets too large it will wrap by setting x to -width
0
 
LVL 92

Expert Comment

by:objects
ID: 16285754
> How does this code work so that it wraps the text around the GUI

it doesn't. the wrapping is handled where:

         if (x > width) {
               x = -width;
          }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16413387
:-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question