This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Label not repainting inside method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Label not repainting inside method." Watch "Label not repainting inside method." New topic
Author

Label not repainting inside method.

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Trying to paint 2 diferent icons in succession with a pause in between, but it only seems to repaint the label on exiting the method.

i.e. in the following code it pauses 4 seconds then changes the label to show the WARRIOR icon, and if I swap the images around only the black is shown.

Any ideas?

Thanks in advance

Paul.

final void TowerShow {
ti.ShowImage(Tower.picLabel, ti.BLACK);
Tower.picLabel.repaint();
Pause(4); //Pauses 4 Seconds - def. works
ti.ShowImage(Tower.picLabel, ti.WARRIOR);
}
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, I guess you are doing this on the event dispatch thread, which is also responsible for redrawing the GUI. So as long as your method runs, the GUI cannot be redrawn.

If that's the case, you will need to execute that code on a different thread.

Moving to our Swing forum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
probably not a real good example for a thread (purpose only to sleep for 4000),
but worth doing just to see the difference (for future reference).

a Timer (set to 4000, repeats(false)) might be better suited.

have a go at a thread first - just remember, at the end of the thread, when
you update the gui's icon, do it via SwingUtilities.invokeLater()
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
A simple example:



It's a bad example. The point is you need to pass what you want to do off to another thread so it doesn't hold up the EDT. Everything GUI related needs to be executed on the EDT, so if you block it then no painting can occur. So you pass what you want to do off to another thread and when that code is executed on that other thread it passes the GUI code back to the EDT using SwingUtilities.invokeLater().
Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Okay, that works great, but what if have to change the picture more than once - do I have to start nesting threads, or is there a way a can send these to a new method that can line up these threads to run sequentially to get the effect:

showpicture1
playsound1
pause1

showpicture2
playsound2
pause2

if (whatever) {
showpicture2
playsound2
pause2
}

Excuse my pidgeon tech speak code but I'm just getting to grips withj Java let alone threads - ouch!

Regards

Paul.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
simple demo for the images
similar for the sounds

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Wahhhh - Still can't get it working.

Having a serious amount of trouble getting this to work with buttons

Currently I'm setting up a general tower event to handle the button methods which goes off into another class and returns an array, with each element having a picture with an associated pause length and sound to play via the method ShowPics();

As this doesn't work I can only assume that the problem is that the event is still not returning control to the main form until the button method is exited.

What I can't do however is figure out how to run this code on exiting the button event.


private JPanel CreateTowerButtons(){
/*final int buttonNo;*/
final JButton[] towerButtons = new JButton[btnCaptions.length];

for (int buttonNo=0;buttonNo<towerButtons.length; ++buttonNo) {
final int buttonNo2 = buttonNo;
towerButtons[buttonNo] = new JButton(btnCaptions[buttonNo]);

towerButtons[buttonNo].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tc.pressOK) {
tc.ProcessButton(buttonNo2);
if (tc.picIndex > -1) ShowPics();
}
else System.out.println("press !OK");
}
});
}
return createPane("Whatever", towerButtons);
}




final void ShowPics() {

System.out.println("Showing Pics. PicIndex = " + tc.picIndex);

new Thread(){
public void run(){
for (int td = 0; td <= tc.picIndex; ++td) {
final int td2 = td;
try{Thread.sleep(tc.towerDisplay[td2].pauseNum);}catch(InterruptedException ie){}

SwingUtilities.invokeLater(new Runnable(){
public void run(){

towerLCD.setText(tc.towerDisplay[td2].lCDStr);
tc.ti.ShowImage(Tower.picLabel, tc.towerDisplay[td2].picNum);
tc.ta.PlayAudio(tc.towerDisplay[td2].soundNum);
tc.Pause(tc.towerDisplay[td2].pauseNum);
}
});
}
}
}.start();

tc.pressOK = true;
}
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Try using SwingTimer

A pseudo code


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Got time to finish that pseudo code Now you can plug the class into your code and there you go ..............


[ November 06, 2006: Message edited by: Sam Codean ]
Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Wow! Thanks Sam - seriously appreciate it

So do I just replace my current JLabel with this new Animator class, and call the Animator's Animator(myImages, interval) method from my button event?

Apologies for any obvious questions but I really am a total noob, and I haven't really got a feel for this swing stuff yet.

Regards

Paul.

BTW what is ActionListener actually listening for i.e. what is triggering the event?

Also, what is the line "super();" doing?
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Hmm.... If you need control over the start and stop of animation then you will have to write two methods startAnimation and stopAnimation and start/stop the timer in those methods

I have written the animator that is starts immediately when created so no need ot start (see constructor itself starts the timer)

The Swing Timer triggers an event every "interval" and to handle this event we need an ActionListener implementation. that is what i have provided.

Do read the Java Tutorial on SWing Timer it is worth it
-Sam
Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Hi Sam

Think I've got a grip of what the code is doing, and I don't need to stop the animation so it's pretty much ready to use.

The only thing I still don't understand is the line "super();" I know super is a pointer to the parent class methods, but what is it doing in this context?

Many thanks

Paul.
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
super(); is called the super class constructor and also note that
Animator() is not a method it is the constructor of that class.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Label not repainting inside method.
 
Similar Threads
get a textfield value from a frame to an applet
Pause
Label not repainting inside method.
Can someone explained to me the "paused" state ?
Once paused, Timertask counts at twice the pace