1

I am wondering to know about how to make scrolling text. Just like text which can scroll from right to the left. How to animate text in Java GUI?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • AFAIK, Java is not necessarily designed to make text animations. What you can do is to apply `AffineTransforms` to a `Label` but I'd really doubt that it would look any good. Maybe there are some libraries to support that sort of stuff. – posdef Apr 05 '12 at 12:14
  • http://www.java2s.com/Code/Java/Threads/Swingandthreadsscrolltext.htm – Balaswamy Vaddeman Apr 05 '12 at 12:20
  • Do you want to move Text in a JTextField, in a JLabel, or move the Textfield/ the label, or move Text on the panel? In the last case: graphics.drawString is what you need. – user unknown Apr 05 '12 at 12:24
  • move in JLabel. thank you for your help. i hope it's work – Dina Frinsi megasari Apr 14 '12 at 07:43

1 Answers1

5

maybe not an answer for OP, but I can't see reason, very simple by implements Swing Timer, (may be with Translucent container) and put there a JLabel, (updates to the JLabel could be from Array of Chars to avoids resize of container), for example

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;

public class SlideTextSwing {

    private JWindow window = new JWindow();
    private JLabel label = new JLabel("Slide Text Swing, Slide Text Swing, ..........");
    private JPanel windowContents = new JPanel();

    public SlideTextSwing() {
        windowContents.add(label);
        window.add(windowContents);
        window.pack();
        window.setLocationRelativeTo(null);
        final int desiredWidth = window.getWidth();
        window.getContentPane().setLayout(null);
        window.setSize(0, window.getHeight());
        window.setVisible(true);
        Timer timer = new Timer(20, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int newWidth = Math.min(window.getWidth() + 1, desiredWidth);
                window.setSize(newWidth, window.getHeight());
                windowContents.setLocation(newWidth - desiredWidth, 0);
                if (newWidth >= desiredWidth) {
                    ((Timer) e.getSource()).stop();
                    label.setForeground(Color.red);
                    mainKill();
                }
            }
        });
        timer.start();
    }

    public void mainKill() {
        Timer timer = new Timer(500, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        timer.start();
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                SlideTextSwing windowTest = new SlideTextSwing();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319