AWT Controls - Scrollbar

 // Scrollbar 


import java.awt.*;

import java.awt.event.*;


class Awtcontrols_Demo extends Frame implements AdjustmentListener

{

    Scrollbar sbv, sbh;

    Label L1, L2;

FlowLayout FL;


    Awtcontrols_Demo()

    {

        // Vertical Scrollbar

        sbv = new Scrollbar(

            Scrollbar.VERTICAL,

            0,      // initial position

            2,      // visible amount

            0,      // minimum

            20      // maximum

        );


sbh = new Scrollbar(Scrollbar.HORIZONTAL, 0, 2, 0,25);

sbv.setPreferredSize(new Dimension(10, 200));

sbh.setPreferredSize(new Dimension(200, 10));

        // Label to display position

        L1 = new Label();

L1.setPreferredSize(new Dimension(150,25));

L2 = new Label();

L2.setPreferredSize(new Dimension(150,25));

FL = new FlowLayout();


        add(L1);

add(sbv);

add(L2);

add(sbh);


        // Register AdjustmentListener

        sbv.addAdjustmentListener(this);

sbh.addAdjustmentListener(this);

        setLayout(FL);

        setTitle("Scrollbar Demo");

        setSize(300, 325);

        setVisible(true);


        // Window closing event

        addWindowListener(new WindowAdapter()

        {

            public void windowClosing(WindowEvent e)

            {

                System.exit(0);

            }

        });

    }


    // Handle scrollbar movement

    public void adjustmentValueChanged(AdjustmentEvent ae)

    {

        int positionv = sbv.getValue();

int positionh = sbh.getValue();


        L2.setText("H SB - "+positionh);

L1.setText("V SB - "+positionv);

    }


    public static void main(String args[])

    {

        new Awtcontrols_Demo();

    }

}



Sample Output:




No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu