Hi folks,

I have been making a marquee scroller that reads the text coming from the right to the left, so setting a JLabel's text using setText() starting with 60 spaces and one character-long substring, to zero spaces and 60 character-long substring.

I've noticed that immediately, the jlabel's text starts to 'grow'. The first character of the text is moving to the left one character at a time, but the right hand side is (slower than 1 character at a time) moving to the right. This stops when the start of the text hits the left border of the JLabel (no spaces).

So I've been trying to figure out why. The substring of the displayed text should grow at exactly the same rate as the number of spaces decreases (1 character), so the overall 'length' of the displayed text should always be 60. Therefore the problem suggests to me that a space character is shorter than other characters? However, that sounds very implausible... Can anyone suggest my error?

My code is as follows (this is just a mock-up to putting it in my real GUI application):

k sets the number of spaces, and decreases from 60 to 0.
label.setText(spaces + text.substring(0, 60-k)); sets the jlabel's text, where the issue is occuring.

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

public class AnimateText {

    int i,k;
    JLabel label;
    String text = "Hello this is a test abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvabcdefghijklm" +
            "nopqrstuvwxyzabcdefghijklmnopqrstuvabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv";
    boolean ending,starting;

    String include;

    public static void main( String[] args ) {
        AnimateText animateClass = new AnimateText();
        animateClass.go();
    }

    public void go(){

        ending = false;
        starting = true;
        JFrame frame = new JFrame();
        label = new JLabel("");
        label.setSize(60,30);
        label.setFont(new Font("Serif",Font.PLAIN,18));
        i=0;
        k=60;

        frame.add(label);
        frame.setSize(400,100);
        frame.setVisible(true);

        ActionListener2 listener = new ActionListener2();

        Timer theTimer = new Timer(500, listener);
        theTimer.start();

    }

    private class ActionListener2 implements ActionListener{

        public void actionPerformed (ActionEvent e){

            String spaces ="";
            int a;
            for(a = 0;a<k;a++){
                spaces += " ";
            }

    // This is where the length-increasing starts.
            if(starting==true){
                i--;
                label.setText(spaces + text.substring(0, 60-k));

                k--;
                if(k==0){
                    starting=false;
                }
            }

            else if(starting == false && ending==false){


                if((i+60) == text.length()-1){
                    ending=true;
                }
                else{
                    label.setText(text.substring(i, i+60));
                }

            }
            else if(starting==false && ending==true){

                label.setText(text.substring(i,text.length()));

                if(i==text.length()){
                    ending=false;
                    starting=true;
                    i=0;
                    k=59;
                }

            }
            i++;
        }

    }


}

The place where the issue is occuring is the if statement where starting==true. Hopefully (for your sake) you can ignore most of the rest and just look at that snippet.

Thanks for any advice/hints. It is much appreciated.

Richard

Recommended Answers

All 2 Replies

The default font for a JLabel is porportionally-spaced, so , for example an "i" or a space (" ") will be lot narrower than an "M" or a "W".
If you want all characters (including " ") to be the same space specify a fixed-width font for your JLabel, eg Courier.

ps: For a good scrolling effect create your own subclass of JLabel and override paintComponent. Use drawString(...) to draw the text at a position that you can change 1 pixel at a time to slide the text right or left. That works just the same with porportionally spaced fonts.

Thankyou so much, it worked perfectly. I completely forgot not all fonts are monospace.

I will look into your suggestion. The last time I asked for help off this website, a further suggestion set apart from my specific solution turned out to be invaluable :)

Richard

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.