• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Applets » Scrollbar in Java Example
Next →
← Prev

Scrollbar in Java Example

By Dinesh Thakur

A scrollbar is a component that allows a user to select any numeric value by dragging a slider within a range defined by a minimum and maximum. A scrollbar may be oriented horizontally or vertically. A scrollbar consists of several individual parts: arrows (the buttons at each end of the scrollbar), a slider box or thumb (scrollable box you slide) and a track (part of the scrollbar you slide the thumb in).

Whenever the user clicks the arrows on either end, the current value of the scrollbar is modified by a unit depending upon direction of the arrows. The slider box indicates the current value of the scrollbar. The user can also click anywhere on the track to move the slider box to jump in that direction by some value larger than 1. It can be formed with the following constructors:

Scrollbar();
Scrollbar(int style);
Scrollbar(int style. int initialvalue, int thumbsize,int min. int max);
The second constructor creates a scroll bar with specified orientation, that is, horizontal or vertical. The third form specifies the initial value and the maximal value along with the thumb size. After initiating the Scroll with the default constructor, the values can be set using the following method:
void setValues(int initial. int thumbsize. int max);
The methods getValue() and setValue() are used to get and set the value of the scroll bar at a particular instant. The unit increment and block increment can be set using the methods:
void setUnitlncrement(int new)
void setBlocklncrement(int new)
The minimum value and the maximum value of the scrollbar can be obtained using getMinimum( ) and getMaximum( ) methods. There are two types of the scrollbarsvertical and horizontal. When creating scrollbars, the constants Scrollbar.VERTICAL and Scrollbar.HORIZONTAL are used. When a block in the scrollbar is adjusted, AdjustmentEvent is generated and hence the AdjustmentListener interface has to be used to handle the scrollbar. Program illustrates the use of scroll bars .

Scrollbar Interface

Method

Returns

Notes

Scrollbar(),

 

Orientation is either horizontal or vertical; value is starting value; visible is the page size; minimum and maximum are the reporting range for the ends of the bar

Scrollbar(int orientation),

 

 

Scrollbar(int orientation, int value, int visible, int minimum, int maximum),

 

 

getLineIncrement()

Int

How much is each “click” worth?

getMaximum()

Int

What is the largest value the scrollbar will report?

getMiniimum()

Int

What is the smallest value the scrollbar will report?

getOrientation()

Int

Either horizontal or vertical

getPageIncrement()

Int

How much is a page movement worth? 

getValue()

Int

What is the current value of the bar?

getVisible()

Int

How big is the handle(in line increments)

setLineIncrement(int value)

Void

Each click of the up/left, right/down buttons moves the current value this far

setPageIncrement(int value)

Void

Each click of the page up/left, right/down moves the current value this far

setValue(int value)

Void

Set the handle at this value

setValues(int value, int visible, int minimum, int maximum)

Void

Reset the scrollbar to reflect the bounds passed in

 

import java.awt.*; 
class ScrollBarExample extends Frame
{
     ScrollBarExample()
     {
           setLayout(new FlowLayout());
           //Horizontal Scrollbar with min value 0,max value 100,initial value 50 and visible amount 10
           Label lblHor =new Label("Horizontal Scrollbar");
           Scrollbar sl = new Scrollbar(Scrollbar.HORIZONTAL,50,10,0,100);
           //Vertical Scrollbar with min value 0,max value 255,initial value 10 and visible amount 5
           Label lblver =new Label("Vertical Scrollbar");
           Scrollbar s2 = new Scrollbar(Scrollbar.VERTICAL,10,5,0,10);
           add(lblHor); add(sl);
           add(lblver); add(s2);
     }
}
     class ScrollBarJavaExample
     {
           public static void main(String args[])
          {
               ScrollBarExample frame = new ScrollBarExample();
               frame.setTitle("Scrollbar in Java Example");
               frame.setSize(520,200);
               frame.setVisible(true);
          }
     }

Scrollbar

You’ll also like:

  1. Java Swing JTextField Scrollbar Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW