• Java Check box Class

    Java Check box Class

    A Java check box class object is a control that is used to turn an option check or unchecked. User can change the state of a check box by clicking on it. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class.

    Checkbox supports these constructors:

    Checkbox( ) throws HeadlessException

    Checkbox(String str) throws HeadlessException

    Checkbox(String str, boolean on) throws HeadlessException

    Checkbox(String str, boolean on, CheckboxGroup cbGroup) throws HeadlessException

    Checkbox(String str, CheckboxGroup cbGroup, boolean on) throws HeadlessException

    To retrieve the current state of a check box, call getState(). To set its state, call setState(). You can obtain the current label associated with a check box by calling getLabel(). To set the label, call setLabel().

    if setState(boolean on) is return true, the box is checked. If it returns false, the box is cleared. The string setLabel(String str) becomes the new label associated with the invoking check box.

    Java Checkbox Program Code

    Exmaple:

    import java.awt.*;

    import java.applet.*;

    import java.awt.event.*;
    public class CheckBoxApplet extends Applet implements ItemListener

    {

        String msg = “”;

        Checkbox chkFirst, chkSecond, chkThird, chkMale, chkFemale;

        CheckboxGroup cbg;


        public void init()

        {

                chkFirst = new Checkbox(“Reading”);

                chkSecond = new Checkbox(“Writing”);

                chkThird = new Checkbox(“Dancing”);


                cbg = new CheckboxGroup();

                chkMale = new Checkbox(“Male”, cbg, true);

                chkFemale = new Checkbox(“Female”, cbg, false);            

                add(chkFirst);

                add(chkSecond);

                add(chkThird);

                add(chkMale);

                add(chkFemale);


                chkFirst.addItemListener(this);

                chkSecond.addItemListener(this);

                chkThird.addItemListener(this);

                chkMale.addItemListener(this);

                chkFemale.addItemListener(this);

        }


        public void itemStateChanged(ItemEvent ie)

        {

                repaint();

        }

        public void paint(Graphics g)

        {

                msg = “Current State : “; 

                g.drawString(msg, 6, 80);

                msg = “Reading : ” + chkFirst.getState();

                g.drawString(msg, 6, 100);

                msg = “Writing : ” + chkSecond.getState();

                g.drawString(msg, 6, 120);

                msg = “Dancing : ” + chkThird.getState();

                g.drawString(msg, 6, 140);

                msg = “Current Selection :”;

                msg += cbg.getSelectedCheckbox().getLabel();

                g.drawString(msg, 6, 200);

        }

    }

    Output:

    Java Check box Example

    Figure: Java Check box Example

    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *