Question

I need to construct a marquee in java application. I thought that a JLabel is a good control to work with. I have done something with horizontal marquee label but making it vertical isn't so easy for me.

Was it helpful?

Solution

You can use a JPanel with a JLabel inside to marque using the class below

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MarqeuePanel extends JPanel implements Runnable
{
    private boolean IsStoped = false;
    private JLabel label;
    public MarqeuePanel(Dimension d,Point p,String text)
    {
        super();
        label = new JLabel();
        label.setText(text);
        add(label);
        setSize(d);
        setOpaque(false);
        setBackground(new Color(0,0,0,0));
        label.setLocation(0,this.getSize().height);
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() 
    {
        //Marqueue
        label.setLocation(0,this.getSize().height);
        while(!IsStoped)
        {
            if(label.getLocation().y < -label.getSize().height)
            {
                label.setLocation(0,this.getSize().height);
            }
            else
            {
                int x = 0;
                int y = label.getLocation().y - 1;
                label.setLocation(x,y);
            }
            try{Thread.sleep(30);}catch(Exception exc){}
        }
    }

    public void StopMarque()
    {
        IsStoped = true;
    }
}

Just careful if you need New Lines ,you can use tag's

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top