R4R Java SourceCode Scrollbar Example Java Applet

Scrollbar Example Java Applet

In this example we will learn how we can create Scrollbar in java. In this example we are creating three  Scrollbar .These Scrollbars are used to set background color. Here we are using Scrollbar class to strong  an object of scrollbar .We are using Scrollbar(int x,int y, int x1,int y1,int m) constructor to create new object of scrollbar. We are using Panel class to create a new panel. In this panel we are adding these three scrollbars.To add the scrollbars at panel we are using add() method. We are using GridLayout and BorderLayout here to set layout .To set  layout we are using setLayout() method. As you can see here we are using two layout. The GridLayout is used to set component  into panel and BorderLayout is used to set the   panel at north

As we know , we can apply AdjustmentListener on Scrollbar. To register on scrollbar we are using addAdjustmentListener(). We are using adjustmentValueChanged(AdjustmentEvent e) to handle the change on scrollbar .The AdjustmentEvent class is used to store the  actions by scrollbars into an object. We are using getValue() method to get the current position. We are taking position of three scrollbar into three variable and finally we are passing these values into Color(int x,int y,int z) Thus we are getting different values from three scrollbars which will gives different color to set the background color . To show three values on status bar we are using showStatus(String str) method.

SOURCE CODE


//<applet code=ScrollbarExample height=300 width=400>
</applet>
import java.applet.*; import java.awt.*; import java.awt.event.*; public class ScrollbarExample extends Applet
implements AdjustmentListener { // style(0-H/1-V),initialvalue,
thumbsize,min,max)
Scrollbar r = new Scrollbar(0,1,20,1,275); Scrollbar g = new Scrollbar(0,1,20,1,275); Scrollbar b = new Scrollbar(0,1,20,1,275); Panel p = new Panel(); public void init() { p.setLayout(new GridLayout(3,1)); setLayout(new BorderLayout()); p.add(r); p.add(g); p.add(b); add(p,"North"); r.addAdjustmentListener(this); g.addAdjustmentListener(this); b.addAdjustmentListener(this); } public void adjustmentValueChanged(AdjustmentEvent e) { int cr,cg,cb; cr =r.getValue(); cg =g.getValue(); cb =b.getValue(); showStatus("Color is --> r = "+cr+" , g = "+cg+" ,
b = "
+cb); setBackground(new Color(cr,cg,cb)); } }

Download Source File
 

Output of This Example