Table of contents
1.
Introduction
2.
AWT Scrollbar
3.
Class declaration
4.
Fields of Scrollbar Class
5.
Constructors of AWT Scrollbar
6.
Methods of AWT Scrollbar
7.
Java AWT Scrollbar Example
7.1.
Example 1
7.2.
Example 2
8.
Frequently Asked Questions
8.1.
What is AWT?
8.2.
Do we have more methods for the AWT scrollbar?
8.3.
Which library package needs to be imported to use awt Scrollbar?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT Scrollbar

Author SHIVAM SINHA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Scrollbar is a GUI component that allows us to see an invisible number of rows and columns. The object of the Scrollbar class can add a horizontal scrollbar and a vertical Scrollbar. Let us learn more about how we can create a scrollbar in Java using AWT.

AWT Scrollbar

The Scrollbar class is a component in the AWT package which extends the Components class.

A Scrollbar can be added to a top-level container like Frame or a component like Panel. It is used to create a horizontal and vertical Scrollbar.

Class declaration

Following is the declaration for java.awt.Scrollbar class:

public class Scrollbar extends Component implements Adjustable, Accessible

Fields of Scrollbar Class

Following are the fields for java.awt.Image class:

static int HORIZONTAL --A constant that indicates a horizontal scroll bar.

static int VERTICAL --A constant that indicates a vertical scroll bar.

Now, let’s see AWT List Class Constructors.

Constructors of AWT Scrollbar

  • orientation is specifying whether the scrollbar will be horizontal or vertical.
  • Value is specifying the starting position of the knob of the Scrollbar on its track.
  • Minimum is specifying the minimum width of the track on which the Scrollbar is moving.
  • Maximum is specifying the maximum width of the track on which the Scrollbar is moving.

Methods of AWT Scrollbar

To perform the operations on the scrollbar, we have so many methods. Some of the important and most used methods are:

Java AWT Scrollbar Example

In the following example, we create a scrollbar using the Scrollbar () and add it to the Frame. This is a very simple example where we are just creating a frame and a scrollbar, and then we have added the Scrollbar to the Frame, and then we are setting the size, visibility, and layout of the Frame.

Example 1

// importing awt package  
import java.awt.*;   
  
public class ScrollbarExample1 {    
  
// class constructor  
ScrollbarExample1() {    
  
       // creating a frame  
            Frame f = new Frame("Scrollbar Example");    
       // creating a scroll bar  
            Scrollbar s = new Scrollbar();    
  
       // setting the position of scroll bar  
            s.setBounds (100, 100, 50, 100);  
  
       // adding scroll bar to the frame  
            f.add(s);    
  
       // setting size, layout and visibility of frame  
            f.setSize(400, 400);  
            f.setLayout(null);    
            f.setVisible(true);    
}    
  
// main method  
public static void main(String args[]) {    
       new ScrollbarExample1();    
}    
}    
You can also try this code with Online Java Compiler
Run Code

Output:

 

Try and compile it by yourself on java online compiler.

Example 2

import java.awt.*;    
import java.awt.event.*;    
  
  
public class ScrollbarExample2 {   
  
       // class constructor   
     ScrollbarExample2() {    
       // creating a Frame with a title   
            Frame frame = new Frame("Example of Scrollbar");    
  
            // creating a final object of Label  
            final Label label = new Label();   
  
              
            label.setAlignment(Label.CENTER);    
            label.setSize(450, 150);    
  
            // creating a final object of Scrollbar class  
            final Scrollbar temp = new Scrollbar();    
  
            // setting the position of scroll bar  
            temp.setBounds(100, 100, 50, 100);    
  
            // adding Scrollbar and Label to the Frame  
            frame.add(s);  
       frame.add(label);      
            frame.setSize(400, 400);    
            frame.setLayout(null);    
            frame.setVisible(true);  
  
       // adding AdjustmentListener to the scrollbar object  
            temp.addAdjustmentListener(new AdjustmentListener() {    
                public void adjustmentValueChanged(AdjustmentEvent e) {    
                   label.setText("Vertical Scrollbar value is:"+ temp.getValue());    
                }    
            });    
         }  
  
// main method    
public static void main(String args[]){    
new ScrollbarExample2();    
}    
}    
You can also try this code with Online Java Compiler
Run Code

Output:

 

In this example, firstly, we have created a Scrollbar and added it to the Frame. We have used the addAdjustmentListener() method of the Scrollbar class, which receives the instances of the AdjustmentListener() method of the Scrollbar class, and eventually, we display it in the form of a label.

Also read, Introduction to Java and Hashcode Method in Java

Frequently Asked Questions

What is AWT?

It stands for Abstract Window Toolkit. It is an API that Java programmers use to create GUI(Graphical User Interface) objects such as scroll bars, windows, and buttons.

Do we have more methods for the AWT scrollbar?

Yes, we have more methods like paramString(), processAdjustmentEvent (AdjustmentEvent e), processEvent(AWTEvent e), removeAdjustmentListener (AdjustmentListener l), setBlockIncrement(int v), setOrientation (int orientation), setUnitIncrement(int v) and many more but in this article, we have just discussed important and most used ones.

Which library package needs to be imported to use awt Scrollbar?

We need to import class import java.awt.Scrollbar;

Conclusion

In this article, we have discussed the AWT List through different examples. We also discussed the constructors and methods of the AWT List.

Enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills for the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you are just a beginner and want to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

You may consider our paid courses curated by experts to give your career an edge over others!

Do Upvote and Happy Learning!

Live masterclass