Im not sure if anyone can help out here but its worth a shot. In the program im writing we are using jogl for animation and as the anmiation is running we would like to have in our main GUI window the ability to use a JLabel as an animation clock to display the correct times of the animation as well as have a JSlider as a progress bar, with the intention of the user being able to move the JSlider to a position and have the animation go forward and back in time. I wrote a small little app to test how I could do this but it doesnt work. I was basically having a for loop to run through the min and max time and every time the for loop would increment i would call the setText() function of the JLabel and the setValue() function of the JSlider to have the values incremented but when i click the button to accomplish it it never updates the labels or the slider until the program is done running through the for loop. Can anyone help me?
Sound like you are running the loop on the AWT event thread - the same thread that processes the repaint events for the label and slider. Because you never return from processing the button press event, those repaint events don’t get a chance to run until the loop is over.
There are two basic ways to fix it:
- Have the button start a swing timer and increment the positon when the timer fires.
or…
- Create another thread to handle the changing of the position, and use SwingUtilities.invokeLater to change the label and slider.
OR your just failing to call repaint()
Every time you change a swing widget’s display you need to call .repaint() to let it know it needs to update 8)
Um, Jeff, that’s not entirely true. I’m suprized you suggested it without more qualifications.
Most Swing components will trigger the repaint event internally when you do things like setText(“hi mom”) on a JLable or setPosition(42) on a JSlider for example.
The code for JLabel’s setText method contains:
public void setText(String text) {
...
if (text == null || oldValue == null || !text.equals(oldValue)) {
revalidate();
repaint();
}
}
Guess I just assume they wont because I have run into them not repainting themselves too often.
Its the safest course though in this case you are correct.
+1 for AWT Event Thread problem.
I assume you have something like this:
final JLabel label= new JLabel();
final JButton button= new JButton();
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i=0; i < 100; i++)
{
label.setText("...");
}
}
}
);
This way, Swing/Awt never gets a chance to act upon the desired changes.
Since Swing and multithreading is a complicated matter, you have to write this code like
final JLabel label= new JLabel();
final JButton button= new JButton();
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new Thread()
{
public void run()
{
for(int i=0; i < 100; i++)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
label.setText("...");
}
}
);
}
}
}.start();
}
}
);
So you have the loop outside the AWT event thread and the setText()-calls injected back in the Event queue via SwingUtilities.invokeLater().
You might split the above code up in declared classes instead of inline declarations to make it more readable.
Just calling repaint on the label might work as well, but is not the recommended way of dealing with such kind of things.
Just for the record setText() is one of the few Swing methods that is guaranteed to be safe to call from outside the event thread. Not that it helps in this case, since there is that slider to update as well.