Home arrow Java & J2EE arrow Page 3 - Java's Advanced User Interface Components

Scrollbars and Sliders - Java

javaLast time, we mastered some basic UI components and worked on how to add them to panels, organize their layout, and manage their events. Having covered all the basic techniques that we’ll need to get started with Java interface programming in our first article, Java's Basic User Interface Components, we will move on to some advanced UI components. Today, I’ll be covering scrolling lists, scrollbars, and canvases, and their functionality. It is amazing to see how many of complex tasks are simplified with the help of these advanced user interface components.

  1. Java's Advanced User Interface Components
  2. More on the List Class
  3. Scrollbars and Sliders
  4. Canvases
By: Gayathri Gokul
Rating: starstarstarstarstar / 18
November 11, 2003

print this article


Scrollbars are components that enable us to select a value by sliding a box between two arrows. We have already seen that text areas and scrolling lists come with their own scrollbars. These UI components have built-in scrollbars functionality, which enables us to manage both the text area and the list and its scrollbar as a single unit. We can also create individual scrollbars or sliders to manipulate a range of values. The scrollbar class is used for other scrollbars, and a scrollbar can be either horizontal or vertical
Scrollbars are normally created by specifying the maximum and a minimum value using the components. Scrollbars are visual elements, hence choosing any of these visual elements cause a change in the scrollbars value; we don't have to update anything or handle any events. We just have give the scrollbar a maximum and minimum, and Java will handle the rest. To change the current value of that scrollbar, you can use three different parts of the scrollbar given below.
  • The arrows on either end, which increment or decrement the values by some small unit (1 by default).

  • A range in the middle, which increments or decrements the value by a larger amount (10 by default).

  • A box in the middle, often called a thumb, whose position shows where in the range of values the current value is located. Moving this box with the mouse causes an absolute change in the value, based on the position of the box within the scrollbar.

To create a scrollbar, you can use one of three constructors:
  • Scrollbar () creates a scrollbar with its initial maximum and minimum values both 0, in a vertical orientation.

  • Scrollbar (int) creates a scrollbar with its initial maximum and minimum values both 0. The argument represents an orientation, for which you can use the class variables Scrollbar.HORIZONTAL and Scrollbar.VERTICAL.

  • Scrollbar( int, int, int, int, int) creates a scrollbar with the following arguments (each one is an integer, and they must be presented in this order):

    • The first argument is the orientation of the scrollbar either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL.

    • The second argument is the initial value of the scrollbar, which should be a value between maximum and minimum values of a scrollbar.

    • The third argument is the overall width or height, depending on the orientation of the scrollbars box (applies best to things such as windows and text areas).

    • The fourth and fifth arguments are the minimum and maximum values for the scrollbar respectively.

{mospagebreak title=More on Scrollbars and Sliders} The following example shows an elegant yet easy applet that displays a scrollbar. The GridLayout object is used with setLayout() method of the applet to provide a layout in which a scrollbar occupies its entire container. Please note no matter what values are supplied for the applet’s height and width, the scrollbar will fill the entire area.

<Applet code “SliderTest.class”
Width = 200
Height = 200>

Import java.awt.*;

Public class SliderTest extends java.applet.Applet
 Scrollbar sbar = new Scrollbar (Scrollbar.HORIZONTAL, 50,0,1,100);
 GridLayout g = new GridLayout(1,1)

 Public void init ()
   add (sbar);

The scrollbar class provides several methods for managing the values within the scrollbar they are as follows:

Method Action
getMaximum() Returns the maximum value.
getMinimum() Returns the minimum value.
getOrientation() Returns the orientation of a scrollbar
getValue() Returns the scrollbar's current value.
setValue(int) Sets the current value of the scrollbar
setLineIncrement(int inc) Change the increment for how far to scroll when the endpoints of the scrollbar are selected. The default is 1.
getLineIncrement() Returns the increment for how far to scroll when the endpoints of the scrollbar are selected.
getPageIncrement() Returns the increment for how far to scroll when the inside range of the scrollbar is selected.
setPageIncrement(int inc) Change the increment for how far to scroll when the inside range of the scrollbar is selected.

The Following example is about a simple scrollbar that increments a single value. The label to the left of the scrollbar is updated each time the scrollbar's value changes. The arguments to the Insets constructor provide pixel insets for the top, bottom, left, and right edges of the component, respectively. This particular example provides an inset of 15 pixels on all four sides. The Scrollbar increment events are handled using special handleEvent() method. That object is stored in the event's target instance variable, and we can use the instanceof operator to find out what kind of UI component sent it. We use getValue() method to return the scrollbar’s current value.

<Applet code “ScrollTest.class”

 Width = 200 Height = 200>

Import java.awt.*;

Public class ScrollTest extends java.applet.Applet {
 Label lab;

 Public void init() {
   setLayout(new GridLayout(1,2));
   lab = new Label("0", Label.CENTER);
   add(new Scrollbar(Scrollbar.VERTICAL,0,0,1,100));

 Public Insets insets() {
   return new Insets(15,15,15,15);

 Public boolean handleEvent(Event evnt) {
   if (evnt.target instanceof Scrollbar)
     int i = ((Scrollbar)evnt.target).getValue();
     return true;
  else return false;


>>> More Java & J2EE Articles          >>> More By Gayathri Gokul

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: