/* 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. =============  OwnComponentSw.java  ================== 

2. import javax.swing.* ; 

3. import java.awt.*; 

4. import java.awt.event.*; 

5.  

6. public class OwnComponentSw 

7.     extends JPanel 

8.     implements MouseListener 

9.

10.   Container parent ; 

11.  

12.   public void 

13.   paintComponent ( Graphics gr ) 

14.   { 

15.     super.paintComponent( gr ); 

16.     gr.drawString ( "I can take care of myself", 50, 50 ) ; 

17.   } 

18.  

19.   public 

20.   OwnComponentSw( Container par ) 

21.   { 

22.     setPreferredSize( new Dimension(200,100) ); 

23.     setOpaque(true); 

24.     parent = par ; 

25.     setBackground ( Color.green ) ; 

26.     addMouseListener ( this ) ; 

27.   } 

28.   public void 

29.   mouseEntered ( MouseEvent ev ) 

30.   { 

31.     setBackground ( Color.pink ) ; 

32.   } 

33.  

34.   public void 

35.   mouseExited ( MouseEvent ev ) 

36.   { 

37.     setBackground ( Color.yellow ) ; 

38.   } 

39.  

40.   public void mouseClicked(MouseEvent ev) { } 

41.   public void mousePressed(MouseEvent ev) { } 

42.   public void mouseReleased(MouseEvent ev) { } 

43.  

44.

45. ============  CustomcompSw.java  ================ 

46. import javax.swing.* ; 

47. import java.awt.* ; 

48. import java.awt.event.*; 

49.  

50. public class CustomcompSw 

51.     extends JApplet 

52.     implements MouseListener 

53.

54.   OwnComponentSw oc ; 

55.   JTextArea ta ; 

56.   JScrollPane sp ; 

57.   Container cp ; 

58.  

59.   public 

60.   CustomcompSw( ) 

61.   { 

62.     oc = new OwnComponentSw ( this ) ; 

63.     ta = new JTextArea ( 5,50 ) ; 

64.     sp = new JScrollPane ( ta ); 

65.   } 

66.  

67.   public void 

68.   init() 

69.   { 

70.     cp = getContentPane(); 

71.     cp.setBackground ( Color.orange ) ; 

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

73.     cp.add ( oc ) ; 

74.     oc.addMouseListener ( this ) ; 

75.  

76.     cp.add ( sp ) ; 

77.   } 

78.  

79.   public void 

80.   mouseEntered ( MouseEvent ev ) 

81.   { 

82.     ta.setText ( "I'm listening, too" ) ; 

83.     ta.append ( "\n" + ev ) ; 

84.     ta.setCaretPosition (0); 

85.   } 

86.   public void mouseClicked(MouseEvent ev) { } 

87.   public void mouseExited(MouseEvent ev) { } 

88.   public void mousePressed(MouseEvent ev) { } 

89.   public void mouseReleased(MouseEvent ev) { } 

90.

91.