Can't update GUI from thread - need syntax for invokeLater
Paul Carter
Ranch Hand
Joined: Sep 20, 2006
Posts: 57
posted
0
Serious Greenhorn here...
Been messing around for a while, but can't get a label to change between 2 colours with a pause in the middle when running via the ActionListener thread created for the buttons (my actual requirement is more elaborate than that, but this illustrates the problem).
As I understand it the ActionListener for the buttons is a thread, and as such does not update the GUI until it has completed i.e. only the last colour change is effective.
I've had a few suggestions, but can't seem to get them to work. I think I just need to see it applied to my problem for it to make sense.
As such can someone just edit this example to show how you would get the method DOCOLOURCHANGES to update the GUI correctly i.e. change the colours of a label with a pause in between.
Major thanks in advance
Paul.
P.S. Apologies if there are any glaring errors - I have edited the code to simplify in places and may have knocked something out - my actual code does work (minus the first colour change).
/* * Tower.java * * Created on 28 July 2006, 11:02 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
//Set up the 1st Controls container Container MainTowerContainer = MainTowerFrame.getContentPane();
//Populate the MainContainer control MainTowerContainer.add(new Tower(MainTowerFrame));
//Diplay The Window MainTowerFrame.pack(); MainTowerFrame.setVisible(true); }
/** * @param args the command line arguments */ public static void main(String[] args) { //Schedule thread to show GUI javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { CreateAndShowTowerGUI(); } }); } }
final void DOCOLOURCHANGES(); { picLabel.setIcon(new ImageIcon("C:/DT/images/BLACK.jpg)); sleep(1000); picLabel.setIcon(new ImageIcon("C:/DT/images/RED.jpg)); } [ January 10, 2007: Message edited by: Paul Carter ]
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
here's a simple demo from your description (the delay has to be in a separate thread, otherwise the gui painting is blocked, then the gui updated via SwingUtilities...)
but it may be a bit easier for you if you used a timer
Paul Carter
Ranch Hand
Joined: Sep 20, 2006
Posts: 57
posted
0
Thanks for that, but still can't get the syntax right for the second image i.e.
new Thread(){ public void run(){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ picLabel.setIcon(new ImageIcon("C:/DT/images/BEAST.jpg")); } }); try{Thread.sleep(1000);}catch(InterruptedException ie){ie.printStackTrace();}
} NOW SHOW SECOND IMAGE AFTER THE ABOVE PAUSE }.start();
Paul Carter
Ranch Hand
Joined: Sep 20, 2006
Posts: 57
posted
0
Ah wait.... Have something that seems to show it working in principle.
I now need to play a little bit.
Many thanks
Paul.
private void MiniTest(int pauseVal, String picName) { new Thread(){ public void run(){ try{Thread.sleep(1000);}catch(InterruptedException ie){ie.printStackTrace();}