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

Create Choice control and implement ItemListener

  1. /*
  2. Choice Control:-
  3. ~~~~~~~~~~~~~~
  4.  Choice() throws java.awt.HeadlessException;
  5. Methods:-
  6. ~~~~~~~
  7.     public int getItemCount();
  8.     public int countItems();
  9.  public String getItem(int);
  10.  final String getItemImpl(int);
  11.     public void add(String);
  12.     public void addItem(String);
  13.  public void insert(String, int);
  14.     public void remove(String);
  15.     public void remove(int);
  16.     public void removeAll();
  17.  public synchronized String getSelectedItem();
  18.     public synchronized Object getSelectedObjects()[];
  19.     public int getSelectedIndex();
  20.     public synchronized void select(int);
  21.     public synchronized void select(String);
  22.     public synchronized void addItemListener(ItemListener);
  23. */
  24.  
  25. import java.awt.event.*;
  26. import java.applet.*;
  27. import java.awt.*;
  28.  
  29. /*
  30. <applet CODE = "ChoiceDemo" Height = 400 Width = 400>
  31. </applet>
  32. */
  33. public class  ChoiceDemo extends Applet implements ItemListener
  34. {
  35.  Choice IceCream;
  36.  Choice Pizza;
  37.  String Itms;
  38.  
  39.  public void init()
  40.  {
  41.   IceCream = new Choice();
  42.   Pizza = new Choice();
  43.  
  44.   // Add Items in IceCream List
  45.   IceCream.add("Venila");
  46.   IceCream.add("Mango");
  47.   IceCream.add("Cherry");
  48.  
  49.   // Add Items in Pizza List
  50.   Pizza.add("Italian");
  51.   Pizza.add("Maxican");
  52.   Pizza.add("American");
  53.  
  54.   add(IceCream);
  55.   add(Pizza);
  56.   IceCream.addItemListener(this);
  57.   Pizza.addItemListener(this);
  58.  
  59.  }
  60.  
  61.  public void itemStateChanged(ItemEvent ie)
  62.  {
  63.   repaint();
  64.  }
  65.  
  66.  public void paint(Graphics g)
  67.  {
  68.   Itms = "Your Order : ";
  69.   Itms += IceCream.getSelectedItem();
  70.   Itms += " : ";
  71.   Itms += Pizza.getSelectedItem();
  72.   g.drawString(Itms, 50, 150);
  73.  }
  74. }
No votes yet
Share this

Post new comment

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