I’m trying to write a Java applet that will display a user-inputted String, it should then flash the string by switching between that and another one. I’m trying to use a thread to do this, but I’m new to those and Applets, I don’t know what to do from here on out.
here’s what I have so far:
public class FlashingLabel extends JApplet implements Runnable
{
String blank;
String input;
JLabel label;
Thread t;
public void init()
{
input = JOptionPane.showInputDialog(null, "Enter String", "Flashing Label", JOptionPane.QUESTION_MESSAGE);
blank=" ";
t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
label=new JLabel(blank);
add(label);
this.repaint();
label=new JLabel(input);
add(label);
this.repaint();
}
}
}
JLabels each time you want to update the message, simple useJLabel#setText. You’re code is currently adding two new labels on each loop throughFor you’re needs, a simple
javax.swing.Timerwill perform the task you are trying to achieve.You might like to have a read through Concurrency in Swing