Posted by Rob Camick on April 24, 2011
A marquee is used to scroll a text message across the screen. The marquee components I’ve seen generally take a text string as a parameter for the marquee class. Then the plain text is scrolled across the screen. Wouldn’t it be nice to jazz up your marquee by using stylized text or by adding images to the marquee?
The MarqueePanel may be what you are looking for. It takes a different approach from other marquee components in that it doesn’t use a string to control the marquee. Instead it uses components. Thats right, just like you would add components to any other panel you add components to the MarqueePanel. Then the MarqueePanel will scroll the components to make it look like a marquee.
By using components you have much more flexibility to create a jazzy marquee. Of course, you can just add JLabel with some text for a simple marquee. But now you can also use a JLabel that uses HTML for its text. This means you can have different sized and colored fonts. If you don’t like using HTML you can try using a JTextPane and use the styled attributes available in the editor kit to give you a special look.
Also, you are not limited to a single component. The MarqueePanel uses a BoxLayout so you can add as many components as you want and they will be scrolled in a single line. The basic code I used to create the MarqueePanel used in the Webstart demo below is something like:
MarqueePanel marquee = new MarqueePanel();
marquee.add( new JLabel( "add some text here" ) );
marquee.add( new JLabel( new ImageIcon(...) ) );
I played with the label font to make it a little bigger, but I’m sure you get the idea. Then you add the marquee to your window and away you go. The scrolling will start as soon as the window is visible.
Of course it would also be nice to control some of the scrolling properties of the marquee. Here is a list of the methods that allow you to do so:
- setScrollFrequency – controls how many times in a second the components are scrolled. Defaults to 5.
- setScrollAmount – controls the scroll amount in pixels each time the components are scrolled.
- setPreferredWidth – controls the preferred width of the marquee. A value of -1 will cause the default preferred width calculation to be used which will use half of the preferred width of the component.
- setWrap – this allows components that have already scrolled off the left edge of the panel to be displayed again on the right edge of the panel. So rather than waiting for all components to completely scroll off the left edge you can have a more continous scroll.
- setWrapAmount – specifies the space in pixels between the traling edge of the components on the panel and the starting edge of the components that have wrapped.
- setScrollWhenFocused – scrolling of the components will only occur when the window has focus. When a window is deactivated scrolling will pause and then resume from the same place once the window is reactived
The above properties are dynamic. That is, they will take effect the next time the components are scrolled. I guess I should also mention the start(), stop(), pause() and resume() methods which are used to control the scrolling of the components. Hopefully the method names are self explanatory.
The magic for this implementation is achieved by using the Graphics2D.translate method
and by taking advantage of the Swing painting mechanism. I hope I haven’t broken any painting rules with this implementation. Check out the provided links below for more information.