/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.*; 

2. import java.awt.* ; 

3.  

4. import java.awt.event.*; 

5.        

6. public class ThreadSwing 

7.     extends JApplet 

8.     implements  Runnable,  MouseListener 

9.

10.   Thread task ; 

11.   boolean pause = false

12.   JLabel lbl, lbl2 ; 

13.   Container cp; 

14.  

15.   public void 

16.   init() 

17.   { 

18.     cp = this.getContentPane( ); 

19.     cp.setLayout( new FlowLayout( )  ); 

20.     cp.add ( lbl = new JLabel ( "+++++++++++++++++" ) ) ; 

21.     cp.add ( lbl2 = new JLabel ( "#################" ) ) ; 

22.     cp.setBackground ( Color.orange ) ; 

23.      validate () ; 

24.     addMouseListener ( this ) ; 

25.  

26.     task = new Thread ( this ) ; 

27.     task.start () ; 

28.  

29.     lbl2.setText ( "Thread started" ); 

30.   } 

31.  

32.   public void 

33.   run () 

34.   { 

35.     Color [] colors = { Color.cyan, Color.green, Color.pink }; 

36.     int i = 0

37.  

38.     Thread thisth = Thread.currentThread () ; 

39.  

40.     while ( task == thisth ) 

41.     { 

42.       if ( !pause ) 

43.       { 

44.         i = i % 3

45.         updateGUI ( "Continued", lbl, colors[i] ) ; 

46.         i++ ; 

47.          

48.       } 

49.       else 

50.       { 

51.         updateGUI ( "Paused", lbl, null ) ; 

52.       } 

53.       try

54.         Thread.sleep ( 500 ) ; 

55.       } 

56.       catch (InterruptedException e) 

57.       { 

58.         updateGUI ( "Interrupted sleep", lbl2, null ) ; 

59.       } 

60.     } ///end while 

61.  

62.     updateGUI 

63.       ( "About to stop", lbl, Color.lightGray ) ; 

64.     } 

65.  

66.   void updateGUI 

67.   ( final String tx, final JLabel lbl, final Color col ) 

68.   { 

69.     final Runnable doGUI = 

70.       new Runnable() 

71.       { 

72.         public void run() 

73.        { 

74.           if ( tx != null

75.             lbl.setText ( tx )  ; 

76.           if ( col != null

77.             cp.setBackground ( col ); 

78.        } //end inner class 

79.      }; 

80.  

81.     try

82.       SwingUtilities.invokeAndWait ( doGUI ) ; 

83.     } 

84.     catch (Exception e) { 

85.       System.out.println(e); 

86.     } 

87.   } 

88.  

89.   public void 

90.   mouseClicked ( MouseEvent ev ) 

91.   { 

92.     pause = !pause ; 

93.     if ( !pause ) 

94.       task.interrupt(); 

95.   } 

96.   public void 

97.   stop () 

98.   { 

99.     pause = true

100.     task.interrupt(); 

101.   } 

102.   public void 

103.   destroy () 

104.   { 

105.     task.interrupt(); 

106.     task = null

107.   } 

108.  

109.   public void mousePressed(MouseEvent e) {} 

110.   public void mouseReleased(MouseEvent e) {} 

111.   public void mouseEntered(MouseEvent e) {} 

112.   public void mouseExited(MouseEvent e) {} 

113.

114. /*===== other source : ============= 

115. if ( SwingUtilities.isEventDispatchThread() ) 

116.  

117. SwingUtilities.invokeLater 

118. */