Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create Checkboxes and implement ItemListener using applet

  1. /*
  2. CheckBox:-
  3. ~~~~~~~~~~
  4.     Checkbox();
  5.     Checkbox(String);
  6.     Checkbox(String,boolean);
  7.     Checkbox(String,boolean,CheckboxGroup);
  8.     Checkbox(String,CheckboxGroup,boolean);
  9. Medhods:-
  10.     String getLabel();
  11.     void setLabel(java.lang.String);
  12.     boolean getState();
  13.     void setState(boolean);
  14.     Object getSelectedObjects()[];
  15.     CheckboxGroup getCheckboxGroup();
  16.     void setCheckboxGroup(CheckboxGroup);
  17.     void addItemListener(ItemListener);
  18.  
  19. */
  20.  
  21. import java.awt.event.*;
  22. import java.awt.*;
  23. import java.applet.*;
  24.  
  25. /*
  26. <applet CODE = "CheckBoxDemo" Height = 400 Width= 400>
  27. </applet>
  28. */
  29.  
  30. public class CheckBoxDemo extends Applet implements ItemListener
  31. {
  32.  Checkbox c1, c2, c3, c4;
  33.  String msg = "";
  34.  
  35.  public void init()
  36.  {
  37.      c1 = new Checkbox("Processor");
  38.      c2 = new Checkbox("Monitor");
  39.      c3 = new Checkbox("Keyboard");
  40.      c4 = new Checkbox("Mouse");
  41.      add(c1);
  42.      add(c2);
  43.      add(c3);
  44.      add(c4);
  45.      c1.addItemListener(this);
  46.      c2.addItemListener(this);
  47.      c3.addItemListener(this);
  48.      c4.addItemListener(this);
  49.  }
  50.  
  51.  public void itemStateChanged(ItemEvent ce)
  52.  {
  53.      msg = "";
  54.      if(c1.getState())
  55.      {
  56.          msg += "Processor, ";
  57.      }
  58.      if(c2.getState())
  59.      {
  60.          msg += "Monitor, ";
  61.      }
  62.      if(c3.getState())
  63.      {
  64.          msg += "KeyBoard, ";
  65.      }
  66.      if(c4.getState())
  67.      {
  68.          msg += "Mouse, ";
  69.      }
  70.      repaint();
  71.  }
  72.  
  73.  public void paint(Graphics g)
  74.  {
  75.      g.drawString("You bought : " +msg, 100, 100);
  76.  }
  77. }
No votes yet
Share this

Post new comment

2 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.