How to add marquee behaviour to text of JLabel
?
I have tried this
JLabel search = new JLabel("<html><marquee>Search</marquee><html>");
but its not working.
A simple marquee can be used by adding a JLabel with text. A fancier marquee might use a JLabel with HTML so you can use different fonts and color for the text. You can even add a second component with an image.
To create a JLabel with an image icon we can either pass an ImageIcon as a second parameter to the JLabel constructor or use the JLabel. setIcon() method to set the icon.
The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly.
Please see http://forums.sun.com/thread.jspa?forumID=57&threadID=605616 for details about how to do this :)
(Edit: I would probably use System.currentTimeMillis() directly inside the paint() method instead of using a timer, and then divide / modulo (%) it to get it into the required range for 'x offset' on the examples). By increasing the size of the division number, you can change the speed ((System.currentTimeMillis() / 200) % 50).
(Edit 2: I just updated the code below to fix a problem with repainting. Now we schedule a repaint within the paint method; maybe there's a better way but this works :))
(Edit 3: Errr, I tried with a longer string and it messed up. That was easy to fix (increase range by width again to compensate for negative values, subtract by width)
package mt;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JLabel;
public class MyJLabel extends JLabel {
public static final int MARQUEE_SPEED_DIV = 5;
public static final int REPAINT_WITHIN_MS = 5;
/**
*
*/
private static final long serialVersionUID = -7737312573505856484L;
/**
*
*/
public MyJLabel() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param image
* @param horizontalAlignment
*/
public MyJLabel(Icon image, int horizontalAlignment) {
super(image, horizontalAlignment);
// TODO Auto-generated constructor stub
}
/**
* @param image
*/
public MyJLabel(Icon image) {
super(image);
// TODO Auto-generated constructor stub
}
/**
* @param text
* @param icon
* @param horizontalAlignment
*/
public MyJLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
// TODO Auto-generated constructor stub
}
/**
* @param text
* @param horizontalAlignment
*/
public MyJLabel(String text, int horizontalAlignment) {
super(text, horizontalAlignment);
// TODO Auto-generated constructor stub
}
/**
* @param text
*/
public MyJLabel(String text) {
super(text);
}
/* (non-Javadoc)
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
@Override
protected void paintComponent(Graphics g) {
g.translate((int)((System.currentTimeMillis() / MARQUEE_SPEED_DIV) % (getWidth() * 2)) - getWidth(), 0);
super.paintComponent(g);
repaint(REPAINT_WITHIN_MS);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With