AWT Scrollbar

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java AWT Scrollbar

The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns.

AWT Scrollbar class declaration

  1. public class Scrollbar extends Component implements Adjustable, Accessible  

Java AWT Scrollbar Example

  1. import java.awt.*;  
  2. class ScrollbarExample{  
  3. ScrollbarExample(){  
  4.             Frame f= new Frame("Scrollbar Example");  
  5.             Scrollbar s=new Scrollbar();  
  6.             s.setBounds(100,10050,100);  
  7.             f.add(s);  
  8.             f.setSize(400,400);  
  9.             f.setLayout(null);  
  10.             f.setVisible(true);  
  11. }  
  12. public static void main(String args[]){  
  13.        new ScrollbarExample();  
  14. }  
  15. }  

Java AWT Scrollbar Example with AdjustmentListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. class ScrollbarExample{  
  4.      ScrollbarExample(){  
  5.             Frame f= new Frame("Scrollbar Example");  
  6.             final Label label = new Label();          
  7.             label.setAlignment(Label.CENTER);  
  8.             label.setSize(400,100);  
  9.             final Scrollbar s=new Scrollbar();  
  10.             s.setBounds(100,10050,100);  
  11.             f.add(s);f.add(label);   
  12.             f.setSize(400,400);  
  13.             f.setLayout(null);  
  14.             f.setVisible(true);  
  15.             s.addAdjustmentListener(new AdjustmentListener() {  
  16.                 public void adjustmentValueChanged(AdjustmentEvent e) {  
  17.                    label.setText("Vertical Scrollbar value is:"+ s.getValue());  
  18.                 }  
  19.             });  
  20.          }  
  21. public static void main(String args[]){  
  22. new ScrollbarExample();  
  23. }  
  24. }