Still struggling with threads ... I Tried moving the inner workings into a thread but still no joy
In the following example I get the messages:
Before
Test Thread
Outside TestThread
I get a 4 second pause then
C:/bazaar.jpgBefore PauseAfter PauseC:beast.jpgBefore PauseAfter PauseC:bazaar.jpgAfter Test Thread
Only the last image change is effective i.e. the screen is still not aware of changes until the thread finishes.
That's why someone suggested the Animator which now works
Would still like to know why the thread doesn't change the image though.
private JPanel CreateTowerButtons(){
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) {
System.out.println("Before Test Thread");
Thread t = new Thread() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestThread();
System.out.println("After Test Thread");
}
});
}
};
t.start();
}
System.out.println("Outside TestThread");
}
});
}
return createPane("Whatever", towerButtons);
}
final void TestThread() {
Tower.picLabel.setIcon(new ImageIcon("C:/beast.jpg"));
Tower.picLabel.repaint();
System.out.print("C:/bazaar.jpg");
Tower.picLabel.setIcon(new ImageIcon("C:/bazaar.jpg"));
Tower.picLabel.repaint();
System.out.print("Before Pause");
tc.Pause(2);
System.out.print("After Pause");
System.out.print("C:beast.jpg");
Tower.picLabel.setIcon(new ImageIcon("C:/beast.jpg"));
Tower.picLabel.repaint();
System.out.print("Before Pause");
tc.Pause(2);
System.out.print("After Pause");
System.out.print("C:bazaar.jpg");
Tower.picLabel.setIcon(new ImageIcon("C:/bazaar.jpg"));
Tower.picLabel.repaint();
}
public void Pause(long s) {
try {
Thread.currentThread().sleep(s * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}