FileDocCategorySizeDatePackage
ChoicePanel.javaAPI DocExample6072Sat Sep 12 03:01:00 BST 1998borland.samples.beans.customizer

ChoicePanel

public class ChoicePanel extends JPanel

Fields Summary
private ButtonGroup
bg
private GridLayout
gl
private JRadioButton
item1
private JRadioButton
item2
private JRadioButton
item3
private JRadioButton
item4
private JRadioButton
item5
private transient Vector
actionListeners
Constructors Summary
public ChoicePanel()


    
    try  {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  
Methods Summary
public synchronized voidaddActionListener(java.awt.event.ActionListener l)

    Vector v = actionListeners == null ? new Vector(2) : (Vector) actionListeners.clone();
    if (!v.contains(l)) {
      v.addElement(l);
      actionListeners = v;
    }
  
voidchoiceChanged(java.awt.event.ItemEvent e)


    if(e.getStateChange() == ItemEvent.SELECTED){
      System.out.println("Selected Logic entered");
      if(item1.isSelected())
        fireActionPerformed(new ActionEvent(this, 1, "item1Selected"));

      else if(item2.isSelected())
        fireActionPerformed(new ActionEvent(this, 2, "item2Selected"));

      else if(item3.isSelected())
        fireActionPerformed(new ActionEvent(this, 3, "item3Selected"));

      else if(item4.isSelected())
        fireActionPerformed(new ActionEvent(this, 4, "item4Selected"));

      else if(item5.isSelected())
        fireActionPerformed(new ActionEvent(this, 5, "item5Selected"));
    }
  
protected voidfireActionPerformed(java.awt.event.ActionEvent e)

    if (actionListeners != null) {
      Vector listeners = actionListeners;
      int count = listeners.size();
      for (int i = 0; i < count; i++)
        ((ActionListener) listeners.elementAt(i)).actionPerformed(e);
    }
  
public intgetSelectedItem()

    Enumeration e = bg.getElements();
    int which = 0;
    JRadioButton jr = null;

    try{
      while(e.hasMoreElements()){
        which +=1;
        jr = (JRadioButton)e.nextElement();
        if( jr.isSelected() )
          break;
      }
    }
    catch(NoSuchElementException x){
      x.printStackTrace();
      return 0;
    }

    return which-1;
  
voidjbInit()

    this.setLayout(gl);

    bg.add(item1);    bg.add(item2);
    bg.add(item3);    bg.add(item4);
    bg.add(item5);

    this.add(item1, null);    this.add(item2, null);
    this.add(item3, null);    this.add(item4, null);
    this.add(item5, null);

    item1.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choiceChanged(e);
      }
    });

    item2.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choiceChanged(e);
      }
    });

    item3.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choiceChanged(e);
      }
    });

    item4.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choiceChanged(e);
      }
    });

    item5.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choiceChanged(e);
      }
    });


  
public synchronized voidremoveActionListener(java.awt.event.ActionListener l)

    if (actionListeners != null && actionListeners.contains(l)) {
      Vector v = (Vector) actionListeners.clone();
      v.removeElement(l);
      actionListeners = v;
    }
  
public voidsetSelectedItem(int which)

    Enumeration e = bg.getElements();
    JRadioButton jr = null;
    int i = 0;

    try{
      for( ; i <= which; i++)
        jr = (JRadioButton)e.nextElement();
    }
    catch(NoSuchElementException x){
      x.printStackTrace();
    }
    jr.setSelected(true);