SunQuest
 
       Java
  Home arrow Java arrow Page 3 - Java's Advanced User Interface Compone...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java's Advanced User Interface Components
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2003-11-11

    Table of Contents:
  • Java's Advanced User Interface Components
  • More on the List Class
  • Scrollbars and Sliders
  • Canvases

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Java's Advanced User Interface Components - Scrollbars and Sliders


    (Page 3 of 4 )

    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>
    </applet>
    */

    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 ()
     {
       setLayout(g);
       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>
    </applet>
    */

    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(lab);
       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();
         lab.setText(String.valueOf(i));
         repaint();
         return true;
       }
      else return false;
     }

    }

    More Java Articles
    More By Gayathri Gokul


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup

    BlackBerry VTS




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway