Javatpoint Logo
Javatpoint Logo

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.

It can be added to top-level container like Frame or a component like Panel. The Scrollbar class extends the Component class.

AWT Scrollbar Class Declaration

Scrollbar Class Fields

The fields of java.awt.Image class are as follows:

  • static int HORIZONTAL - It is a constant to indicate a horizontal scroll bar.
  • static int VERTICAL - It is a constant to indicate a vertical scroll bar.

Scrollbar Class Constructors

Sr. no. Constructor Description
1 Scrollbar() Constructs a new vertical scroll bar.
2 Scrollbar(int orientation) Constructs a new scroll bar with the specified orientation.
3 Scrollbar(int orientation, int value, int visible, int minimum, int maximum) Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.

Where the parameters,

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

Method Inherited by Scrollbar

The methods of Scrollbar class are inherited from the following classes:

  • java.awt.Component
  • java.lang.Object

Scrollbar Class Methods

Sr. no. Method name Description
1. void addAdjustmentListener (AdjustmentListener l) It adds the given adjustment listener to receive instances of AdjustmentEvent from the scroll bar.
2. void addNotify() It creates the peer of scroll bar.
3. int getBlockIncrement() It gets the block increment of the scroll bar.
4. int getMaximum() It gets the maximum value of the scroll bar.
5. int getMinimum() It gets the minimum value of the scroll bar.
6. int getOrientation() It returns the orientation of scroll bar.
7. int getUnitIncrement() It fetches the unit increment of the scroll bar.
8. int getValue() It fetches the current value of scroll bar.
9. int getVisibleAmount() It fetches the visible amount of scroll bar.
10. boolean getValueIsAdjusting() It returns true if the value is in process of changing where action results are being taken by the user.
11. protected String paramString() It returns a string representing the state of Scroll bar.
12. protected void processAdjustmentEvent (AdjustmentEvent e) It processes the adjustment event occurring on scroll bar by dispatching them to any registered AdjustmentListener objects.
13. protected void processEvent(AWTEvent e) It processes the events on the scroll bar.
14. void removeAdjustmentListener (AdjustmentListener l) It removes the given adjustment listener. Thus it no longer receives the instances of AdjustmentEvent from the scroll bar.
15. void setBlockIncrement(int v) It sets the block increment from scroll bar.
16. void setMaximum (int newMaximum) It sets the maximum value of the scroll bar.
17. void setMinimum (int newMinimum) It sets the minimum value of the scroll bar.
18. void setOrientation (int orientation) It sets the orientation for the scroll bar.
19. void setUnitIncrement(int v) It sets the unit increment for the scroll bar.
20. void setValue (int newValue) It sets the value of scroll bar with the given argument value.
21. void setValueIsAdjusting (boolean b) It sets the valueIsAdjusting property to scroll bar.
22. void setValues (int value, int visible, int minimum, int maximum) It sets the values of four properties for scroll bar: value, visible amount, minimum and maximum.
23. void setVisibleAmount (int newAmount) It sets the visible amount of the scroll bar.
24. AccessibleContext getAccessibleContext() It gets the accessible context related to the scroll bar.
25. AdjustmentListener[] getAdjustmentListeners() It returns an array of al lthe adjustment listeners registered on the scroll bar.
26. T[] getListeners(Class listenerType) It returns an array if all objects that are registered as FooListeners on the scroll bar currently.

Java AWT Scrollbar Example

In the following example, we are creating a scrollbar using the Scrollbar() and adding it into the Frame.

ScrollbarExample1.java

Output:

java awt scrollbar example 1

Java AWT Scrollbar Example with AdjustmentListener

In the following example, we are creating a Scrollbar and adding it into the Frame. Here, we are using addAdjustmentListener() method of Scrollbar class which receives the instances of AdjustmentEvent and eventually we display it in the form of Label.

ScrollbarExample2.java

Output:

java awt scrollbar example 2





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA