Java AWT Scrollbar
Last Updated :
26 Nov, 2023
Java AWT (Abstract Window Toolkit) is a collection of libraries for developing responsive and user-friendly interfaces in Java Applications. The Scrollbar is a component that allows us to see an unknown number of rows and columns.
Java AWT Scrollbar
The ‘Scrollbar’ class is a part of the ‘java.awt’ package. Java AWT Scrollbars are used for scrolling through content like text, images, or any other data that doesn’t fit completely within the specified display area. The Scrollbar component supports both vertical and horizontal scrolling. It generates events when it is controlled by the user. We can add custom behavior using different methods to improve the functionality of our application.
Syntax of Java AWT Scrollbar
public class Scrollbar extends Component implements Adjustable, Accessible
Constructors of Java AWT Scrollbar
The table represents the Constructors present in the AWT Scrollbar with a description.
Scrollbar() |
This constructor creates a new vertical scroll bar as the default one. |
Scrollbar(int orientation) |
Creates a new scroll bar with the given configuration. |
Scrollbar(int orientation, int value, int visible, int minimum, int maximum) |
Generates a new scroll bar with the specified direction, minimum and maximum values, visible quantity, and initial value. |
The parameters of the given constructors are:
- Orientation: The orientation of the scrollbar specifies whether it will be horizontal or vertical.
- Value: The starting position of the Scrollbar’s knob on its track.
- Minimum: The smallest width of the track on which the scrollbar moves.
- Maximum: The maximum width of the track on which the scrollbar moves.
Fields of Java AWT Scrollbar
The following are the fields of the Scrollbar class:
static int |
HORIZONTAL |
the constant variable used to denote a horizontal scroll bar. |
static int |
VERTICAL |
A constant that denotes a vertical scroll bar. |
Inherited Fields
The Fields of AWT Scrollbar are inherited by:
- java.awt.Component
- java.awt.Adjustable
- java.awt.image.ImageObserver
Java AWT Scrollbar Methods
The followingreturn are the methods associated with the AWT Scrollbar class:
addAdjustmentListener(AdjustmentListener l) |
void
|
Adds the specified adjustment listener to this scrollbar’s AdjustmentEvent instances. |
addNotify() |
void
|
used to instantiate a peer for the scrollbar. |
getAccessibleContext() |
AccessibleContext
|
Used to return the AccessibleContext for the scrollbar. |
getAdjustmentListeners() |
AdjustmentListener[]
|
Returns an array of all the adjustment listeners that have been registered on the scrollbar. |
getBlockIncrement() |
int
|
The scrollbar’s block increment is returned by this method. |
getMaximum() |
int
|
Gets the scrollbar’s maximum value. |
getMinimum() |
int
|
Gets the scrollbar’s minimum value. |
getOrientation() |
int
|
Returns the scrollbar’s orientation. |
getUnitIncrement() |
int
|
Returns the scrollbar’s unit increment. |
getValue() |
int
|
Returns this scrollbar’s current value. |
getValueIsAdjusting() |
bool
|
Detect any changes caused by used inputs it will return a true. |
getVisibleAmount() |
int
|
Obtains the scrollbar’s visible length. |
paramString() |
protected String
|
This method returns a string indicating the status of the scrollbar. |
processAdjustmentEvent(AdjustmentEvent e) |
protected void
|
Processes scrollbar adjustment events by sending them to any registered AdjustmentListener objects. |
processEvent(AWTEvent e) |
protected void
|
The events on this scrollbar are processed. |
removeAdjustmentListener(AdjustmentListener l) |
void
|
In order to prevent it from receiving instances of AdjustmentEvent from this scrollbar, the specified adjustment listener is removed. |
setBlockIncrement(int v) |
void
|
Controls this scrollbar’s block increment. |
setMaximum(int newMaximum) |
void
|
Sets the scrollbar’s maximum value. |
setMinimum(int newMinimum) |
void
|
Sets the scrollbar’s minimum value. |
setOrientation(int orientation) |
void
|
Sets the scrollbar’s orientation. |
setUnitIncrement(int v) |
void
|
Used to increase the size of scrollbar 1. |
setValue(int newValue) |
void
|
Sets this scrollbar’s value to the given value. |
setValueIsAdjusting(boolean b) |
void
|
Sets the valueIsAdjusting property. |
setValues(int value, int visible, int minimum, int maximum) |
void
|
Sets the values of four scrollbar properties: value, visibleAmount, minimum, and maximum. |
setVisibleAmount(int newAmount) |
void
|
Sets the visible amount of the scroll bar. |
Inherited Methods
The Methods included with AWT Scrollbar are inherited by:
- java.awt.Component
- java.lang.Object
Examples of Java AWT Scrollbar
Now let us understand the Java AWT Scrollbar with the help of some examples along with their demo given below:
Example 1 (Basic Scrollbar):
In this example, two basic scrollbars are implemented. One is the default vertical scrollbar and the other is oriented horizontally. Their respective labels are also aligned below them for better understanding.
Below is the implementation of Java AWT Scrollbar:
Java
import java.awt.*;
public class Main {
public static void main(String[] args){
Frame f = new Frame( "Scrollbar Example" );
Label l = new Label( "GeeksforGeeks" );
l.setBounds( 130 , 80 , 200 , 30 );
l.setFont( new Font( "Arial" , Font.BOLD, 20 ));
l.setForeground(Color.GREEN);
f.add(l);
Scrollbar s1 = new Scrollbar();
s1.setBounds( 280 , 140 , 40 , 175 );
f.add(s1);
Label l1 = new Label( "Vertical Scrollbar" );
l1.setBounds( 260 , 330 , 200 , 30 );
f.add(l1);
Scrollbar s2 = new Scrollbar(Scrollbar.HORIZONTAL);
s2.setBounds( 50 , 250 , 175 , 40 );
f.add(s2);
Label l2 = new Label( "Horizontal Scrollbar" );
l2.setBounds( 85 , 330 , 200 , 30 );
f.add(l2);
f.setSize( 400 , 400 );
f.setLayout( null );
f.setVisible( true );
}
}
|
Run the code using the following commands:
javac Main.java
java Main
Output:
Final Output of the Program:
Explanation of the above Program:
In the code snippet given above, we initialize a frame with a simple label titled “GeeksforGeeks”. Two scrollbars s1 and s2 are created. s1 is the default vertical scrollbar and s2 is set to be horizontal using the property Scrollbar.HORIZONTAL in the constructor. Both of them are added to a frame along with their appropriate labels, l1 and l2.
Example 2 (AdjustmentListener):
In Java AWT, adjustmentListener interface is used to handle the events triggered by scrollbars. It responds to the changes in the component’s value whenever the user interacts with it.
In this example, we have added adjustmentListener to both these scrollbars implemented in the above example. Whenever a user operates the scrollbar, the adjustmentValueChanged method is called and the respective labels are updated with their current values.
Below is the implementation of Java AWT Scrollbar:
Java
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args)
{
Frame f = new Frame( "Scrollbar Example" );
Label l = new Label( "GeeksforGeeks" );
l.setBounds( 150 , 80 , 200 , 30 );
l.setFont( new Font( "Arial" , Font.BOLD, 20 ));
l.setForeground(Color.GREEN);
f.add(l);
Scrollbar s1 = new Scrollbar();
s1.setBounds( 300 , 140 , 40 , 175 );
f.add(s1);
Label l1 = new Label( "Vertical Scrollbar" );
l1.setBounds( 260 , 330 , 200 , 30 );
f.add(l1);
Scrollbar s2 = new Scrollbar(Scrollbar.HORIZONTAL);
s2.setBounds( 50 , 250 , 175 , 40 );
f.add(s2);
Label l2 = new Label( "Horizontal Scrollbar" );
l2.setBounds( 65 , 330 , 200 , 30 );
f.add(l2);
s1.addAdjustmentListener( new AdjustmentListener() {
public void adjustmentValueChanged(
AdjustmentEvent e)
{
int val1 = s1.getValue();
l1.setText( "Vertical Scrollbar Value: "
+ val1);
}
});
s2.addAdjustmentListener( new AdjustmentListener() {
public void adjustmentValueChanged(
AdjustmentEvent e)
{
int val2 = s2.getValue();
l2.setText( "Horizontal Scrollbar Value: "
+ val2);
}
});
f.setSize( 450 , 450 );
f.setLayout( null );
f.setVisible( true );
}
}
|
Output:
Final Output of the Program:
Explanation of the above Program:
In the code example given above, a frame is initialized with a simple label and two scrollbars s1 and s2 as seen in the previous example. To both the scrollbars, we have added adjustmentListener to handle the events when the scrollbar is triggered. Inside the adjustmentListener, the adjustmentValueChanged method is overridden, which fetches the current value of the scrollbars. The updated value of these components is dynamically updated in the labels l1 and l2 as their value changes.
Share your thoughts in the comments
Please Login to comment...