Chapter: Java The Complete Reference - The Java Library - Using AWT Controls, Layout Managers, and Menus

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Managing Scroll Bars - AWT Controls

Scroll bars are used to select continuous values between a specified minimum and maximum.

Managing Scroll Bars

 

Scroll bars are used to select continuous values between a specified minimum and maximum. Scroll bars may be oriented horizontally or vertically. A scroll bar is actually a composite of several individual parts. Each end has an arrow that you can click to move the current value of the scroll bar one unit in the direction of the arrow. The current value of the scroll bar relative to its minimum and maximum values is indicated by the slider box (or thumb) for the scroll bar. The slider box can be dragged by the user to a new position. The scroll bar will then reflect this value. In the background space on either side of the thumb, the user can click to cause the thumb to jump in that direction by some increment larger than 1. Typically, this action translates into some form of page up and page down. Scroll bars

are encapsulated by the Scrollbar class. Scrollbar defines the following constructors:

 

Scrollbar( ) throws HeadlessException Scrollbar(int style) throws HeadlessException

Scrollbar(int style, int initialValue, int thumbSize, int min, int max) throws HeadlessException

 

The first form creates a vertical scroll bar. The second and third forms allow you to specify the orientation of the scroll bar. If style is Scrollbar.VERTICAL, a vertical scroll bar is created. If style is Scrollbar.HORIZONTAL, the scroll bar is horizontal. In the third form of the constructor, the initial value of the scroll bar is passed in initialValue. The number of units represented by the height of the thumb is passed in thumbSize. The minimum and maximum values for the scroll bar are specified by min and max.

If you construct a scroll bar by using one of the first two constructors, then you need to set its parameters by using setValues( ), shown here, before it can be used:

 

void setValues(int initialValue, int thumbSize, int min, int max)

 

The parameters have the same meaning as they have in the third constructor just described. To obtain the current value of the scroll bar, call getValue( ). It returns the current

 

setting. To set the current value, call setValue( ). These methods are as follows:

 

int getValue( )

void setValue(int newValue)

 

Here, newValue specifies the new value for the scroll bar. When you set a value, the slider box inside the scroll bar will be positioned to reflect the new value.

 

You can also retrieve the minimum and maximum values via getMinimum( ) and getMaximum( ), shown here:

 

int getMinimum( ) int getMaximum( )

 

They return the requested quantity.

 

By default, 1 is the increment added to or subtracted from the scroll bar each time it is scrolled up or down one line. You can change this increment by calling setUnitIncrement( ). By default, page-up and page-down increments are 10. You can change this value by calling setBlockIncrement( ). These methods are shown here:

 

void setUnitIncrement(int newIncr)

void setBlockIncrement(int newIncr)

Handling Scroll Bars

 

To process scroll bar events, you need to implement the AdjustmentListener interface. Each time a user interacts with a scroll bar, an AdjustmentEvent object is generated. Its getAdjustmentType( ) method can be used to determine the type of the adjustment. The types of adjustment events are as follows:

BLOCK_DECREMENT : A page-down event has been generated.

BLOCK_INCREMENT : A page-up event has been generated.

TRACK : An absolute tracking event has been generated.

UNIT_DECREMENT : The line-down button in a scroll bar has been pressed.

UNIT_INCREMENT : The line-up button in a scroll bar has been pressed.

The following example creates both a vertical and a horizontal scroll bar. The current settings of the scroll bars are displayed. If you drag the mouse while inside the window, the coordinates of each drag event are used to update the scroll bars. An asterisk is displayed at the current drag position. Notice the use of setPreferredSize( ) to set the size of the scrollbars.

 

 

// Demonstrate scroll bars. I

mport java.awt.*;

 

import java.awt.event.*; import java.applet.*; /*

 

<applet code="SBDemo" width=300 height=200> </applet>

 

*/

 

public class SBDemo extends Applet

 

implements AdjustmentListener, MouseMotionListener { String msg = "";

 

Scrollbar vertSB, horzSB;

 

public void init() {

 

int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height"));

 

vertSB = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, height);

vertSB.setPreferredSize(new Dimension(20, 100));

 

horzSB = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, width);

 

horzSB.setPreferredSize(new Dimension(100, 20));

 

add(vertSB);

 

add(horzSB);

 

// register to receive adjustment events

 

vertSB.addAdjustmentListener(this);

 

horzSB.addAdjustmentListener(this);

 

addMouseMotionListener(this);

 

}

 

public void adjustmentValueChanged(AdjustmentEvent ae) { repaint();

 

}

 

     Update scroll bars to reflect mouse dragging. public void mouseDragged(MouseEvent me) {

 

int x = me.getX(); int y = me.getY(); vertSB.setValue(y); horzSB.setValue(x); repaint();

 

}

 

     Necessary for MouseMotionListener

 

public void mouseMoved(MouseEvent me) {

 

}

 

// Display current value of scroll bars.

public void paint(Graphics g) {

 

msg = "Vertical: " + vertSB.getValue();

 

msg += ", Horizontal: " + horzSB.getValue(); g.drawString(msg, 6, 160);

 

// show current mouse drag position g.drawString("*", horzSB.getValue(),

vertSB.getValue());

 

}

 

}

Sample output from the SBDemo applet is shown in Figure 26-6.


Figure 26-6   Sample output from the SBDemo applet

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.