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

2. import javax.swing.* ; 

3. import javax.swing.event.* ; 

4. import java.awt.* ; 

5.  

6. public class ScrollingSw 

7.     extends JApplet 

8.     implements ChangeListener 

9.

10.   JScrollPane sp ; 

11.   JViewport vp ; 

12.   OwnComponentSw  oc ; 

13.   Container cp ; 

14.   JLabel lb ; 

15.  

16.   public void 

17.   init() 

18.   { 

19.     cp = getContentPane() ; 

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

21.     oc = new OwnComponentSw() ; 

22.     sp = new JScrollPane( oc ) ; 

23.     sp.getVerticalScrollBar().setUnitIncrement( 5 ); 

24.  

25.     sp.setPreferredSize( new Dimension(150,175) ) ; 

26.     cp.add ( sp ) ; 

27.  

28.     vp =  sp.getViewport() ; 

29.     vp.addChangeListener( this ) ; 

30.  

31.     lb = new JLabel("----------------------") ; 

32.     cp.add( lb ); 

33.     cp.setBackground ( Color.orange ); 

34.   } 

35.   public void 

36.   stateChanged ( ChangeEvent ev ) 

37.   { 

38.     if ( ev.getSource() == vp ) 

39.       lb.setText( vp.getViewPosition() + "" ); 

40.   } 

41.

42. === OwnComponentSw.java === 

43. import javax.swing.*; 

44. import java.awt.*; 

45.  

46. public class OwnComponentSw 

47.     extends JPanel 

48.

49.   public void paintComponent ( Graphics gr ) 

50.   { 

51.     super.paintComponent( gr ); 

52.  

53.     for ( int i = 0 ; i < 256; i++ ) 

54.     { 

55.       gr.setColor ( new Color ( 255, i, 0 ) ); 

56.       gr.drawLine ( 0, 0, i, 255 ) ; 

57.     } 

58.   } 

59.   public OwnComponentSw() 

60.   { 

61.    setPreferredSize( new Dimension(255, 255) ); 

62.   } 

63.