Search code examples
javaswingjframemarquee

marquee in title bar in JFrame


How can I make the title bar of a JFrame to be Marquee like the marquee in HTML if you use the marquee tag?


Solution

  • God forgive me for the following code

    Put this code in your frame constructor if you want the marquee to start directly after loading:

        int delay = 3000;
        int period = 50;
        Timer timer = new Timer();
    
        timer.scheduleAtFixedRate(new TimerTask() {
            int spaces=0;
            public void run() {
                String title="";
                for (int j = 0; j < spaces; j++) {
                    title+= " " ;
                }
                title+= "Annoying";
                Main.this.setTitle(title);
                spaces=(spaces+1)%50;
            }
        }, delay, period);
    

    UPDATE
    As per the comments, here is another version using swing.Timer

        Timer timer = new Timer(delay,new ActionListener(){
    
            int spaces=0;
    
            public void actionPerformed(ActionEvent e) {
                String title="";
                for (int j = 0; j < spaces; j++) {
                    title+= " " ;
                }
                title+= "Annoying";
                Main.this.setTitle(title);
                spaces=(spaces+1)%50;
    
            }}
        );
        timer.start();
    

    This code is for learning purpose only, please don't use it in a real product.