Voici mon code pour obtenir un texte défilant, de bas en haut. Il marche nikel mais j'aimerais pouvoir afficher autant de texte que je souhaite sans qu'il sorte du JPanel, mais je n'y arrive pas. En clair, j'aimerais que le JLabel que je fais défiler revienne à la ligne si jamais il dépasse la taille du JPanel.
J'ai essayé d'appeler plusieurs fois ma classe en changeant le 'String text' ou en remplaçant mon JPanel par un JTextArea pour utiliser la méthode setLineWrap(true); mais sans succès.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;
 
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class PanelTexteDéfilant extends JPanel
{
	public PanelTexteDéfilant()
	{
		final class JScrollingText extends JLabel 
		{ 
			private int speed;
			private int period; 
			private int y;
 
			// Méthodes surchargées = 2 Constructeurs
			public JScrollingText(String text,int speed) {this(text,speed,100);}
 
			public JScrollingText(String text,int speed,int period) {this(text,speed,period,0);}
 
			public void paintComponent(Graphics g) 
			{
			    if (isOpaque()) {
			        g.setColor(getBackground());
			        g.fillRect(0,0,getHeight(),getWidth());
			    }
			    g.setColor(getForeground());
 
			    FontMetrics fm = g.getFontMetrics();
			    Insets insets = getInsets();
 
			    int width = getWidth() - (insets.top + insets.bottom);
			    int height = getHeight() - (insets.left + insets.right);
 
			    int textHeight = fm.getHeight(); 
			    if (height < textHeight) {
			        height = textHeight;
			    }
			    y %= height;
 
			    int textX = insets.left + (width - fm.stringWidth(getText()))/2; // Centre le texte
			    int textY = insets.top + y; 
 
			    g.drawString(getText(),textX,textY);
			    g.drawString(getText(),textX ,textY + (speed > 0 ? - height : height));
			}
 
			public void start() 
			{
			    Timer timer = new Timer();
			    TimerTask task = new TimerTask() {
			       public void run() {
			            y = y + speed;
			            repaint();
			        }
			    };
			    timer.scheduleAtFixedRate(task,0,period);
			}  
		}
 
		JScrollingText scrollingText1 = new JScrollingText("Coucou",-1);
	    scrollingText1.start();
	    setPreferredSize(new Dimension(200,330));
                 setLayout(new BorderLayout());
	    add(scrollingText1);
	}
}
J'espère que vous aurez des réponses à mon problème.
Merci