Skip to main content

CHECKBOX in Java

ChechBox in GUI component make user to select the choice. It is defined in java,awt package. In applet the checkbox can be added by creating object of class checkbox. For handling event, ItemListener interface is implement in the class and each checkbox enabled with listener addItemListener(this). When user make select an checkbox the  itemStateChanged(ItemEvent ie) method is called for handling the event.


import java.awt.*; // for GUI component.

import java.applet.*; // for applet container

import java.awt.event.*; // for handling event

 

//<applet code="chktxt" width=800 height=400></applet>

 

public class chktxt extends Applet implements ItemListener

{

            String s1,s2,s3,s4,s5;

            Checkbox ChkChen,ChkBhop,ChkMumb,ChkImph,ChkShil;

            TextArea txtar=new TextArea();

            public void init()

            {

                        s1="\nCapital of TamilNadu";

                        s2="\nCapital of Madhya Pradesh";

                        s3="\nCapital of Maharashtra";

                        s4="\nCapital of Manipur";

                        s5="\nCapital of Meghalaya";

                        ChkChen=new Checkbox("Chennai");

                        ChkBhop=new Checkbox("Bhopal");

                        ChkMumb=new Checkbox("Mumbai");

                        ChkImph=new Checkbox("Imphal");

                        ChkShil=new Checkbox("Shillong");

                        add(ChkChen);

                        add(ChkBhop);

                        add(ChkMumb);

                        add(ChkImph);

                        add(ChkShil);

                        ChkChen.addItemListener(this);

                        ChkBhop.addItemListener(this);

                        ChkMumb.addItemListener(this);

                        ChkImph.addItemListener(this);

                        ChkShil.addItemListener(this);

                        add(txtar);

            }

            public void itemStateChanged(ItemEvent ie)

            {

                        repaint();

            }

            public void paint(Graphics g)

            {

                        txtar.setText("Capital\n");

                        if(ChkChen.getState()==true)

                                    txtar.append(s1);

                        if(ChkBhop.getState()==true)

                                    txtar.append(s2);

                        if(ChkMumb.getState()==true)

                                    txtar.append(s3);

                        if(ChkImph.getState()==true)

                                    txtar.append(s4);

                        if(ChkShil.getState()==true)

                                    txtar.append(s5);

            }

}


checkbox



Thank You. Like Share and Subscribe 👍 folowlect.com

Comments