Sunday, July 28, 2013

Scrollbar

Scrollbar allows you to create a scrolling bar that you can select a value from a range of values. The Scrollbar  can generate adjustment event. To receive the event, the application must implement AdjustmentListener interface and register the listener by using the addAdjustmentListener(AdjustmentListener listener) method.

Constructors of Scrollbar:
-Scrollbar()
creates a new vertical scrolling bar.
-Scrollbar(int orientation)
creates a new scrolling bar with specified orientation. The orientation can be Scrollbar.HORIZONTAL or Scrollbar.VERTICAL.
-Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
creates a new scrolling bar with specified orientation, initial value, visible amount, minimum and maximum values.

Useful methods of the Scrollbar class:
-getMaximum()
reads the maximum value of the scrolling bar.
-getMinimum()
reads the minimum value of the scrolling bar.
-getValue()
reads the value of the scrolling bar.
-setBlockIncrement(int v)
sets the block increment of the scrolling bar.
-setMaximum(int v)
sets the maximum value of the scrolling bar.
-setMinimum(int v)
sets the minimum value of the scrolling bar.
-setUnitIncrement(int v)
sets the unit increment of the scrolling bar.
-setValue(int v)
sets the value of the scrolling bar.

Example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame implements AdjustmentListener{
InterfaceShow(String title){
setTitle(title);
setSize(new Dimension(500,300));
setVisible(true);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Scrollbar colorscroll= new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);
colorscroll.setUnitIncrement(5);
colorscroll.setBlockIncrement(15);
colorscroll.addAdjustmentListener(this);
add(colorscroll, BorderLayout.NORTH);

}

public void adjustmentValueChanged(AdjustmentEvent e){
Scrollbar scr=(Scrollbar)e.getSource();
setBackground(new Color(scr.getValue()));

}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using ScrollBar");

}
}

scrollbar

No comments:

Post a Comment