Java AWT Scrollbar


The object-and-class-in-java 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

Output:

java awt scrollbar example 1

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. }  

Output:

java awt scrollbar example 2





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition