Thursday, November 6, 2014

Java using radio button pro



Create AWT Radio Buttons Using Checkbox Group

import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
public class CreateRadioButtonsExample extends Applet
{
public void init()
{
CheckboxGroup lngGrp = new CheckboxGroup();
Checkbox java = new Checkbox("Java", lngGrp, true);
Checkbox cpp = new Checkbox("C++", lngGrp, true);
Checkbox vb = new Checkbox("VB", lngGrp, true);
add(java);
add(cpp);
add(vb);
   }
}

Get Selected Radio Button

import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class GetSelectedRadioButtonExample extends Applet implements ItemListener
{   
CheckboxGroup lngGrp = null;   
public void init()
{
lngGrp = new CheckboxGroup();
Checkbox java = new Checkbox("Java", lngGrp, true);
Checkbox cpp = new Checkbox("C++", lngGrp, false);
Checkbox vb = new Checkbox("VB", lngGrp, false);
                   add(java);
add(cpp);
add(vb);
java.addItemListener(this);
cpp.addItemListener(this);
vb.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
Checkbox chk = lngGrp.getSelectedCheckbox();
g.drawString(chk.getLabel() + " is selected", 10 ,70);
   }
}

Set Selected Radio Button

import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
public class SetSelectedRadioButtonExample extends Applet
{
CheckboxGroup lngGrp = null;       
public void init()
{
lngGrp = new CheckboxGroup();
Checkbox java = new Checkbox("Java", lngGrp, false);
Checkbox cpp = new Checkbox("C++", lngGrp, false);
Checkbox vb = new Checkbox("VB", lngGrp, false);               
add(java);
add(cpp);
add(vb);
lngGrp.setSelectedCheckbox(cpp);
   }
}

Count Number Of Items in a AWT Choice Or Combobox

import java.applet.Applet;
import java.awt.Choice;
import java.awt.Graphics;
public class CountNumberOfItems extends Applet
{
Choice language = null;
public void init()
{
language = new Choice();
language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");
add(language);
}
public void paint(Graphics g)
{
int items = language.getItemCount();
g.drawString("There are " + items + " items in a combobox", 10, 70);
   }
}

Create AWT Choice Or Combo box

import java.applet.Applet;
import java.awt.Choice;
public class CreateChoiceExample extends Applet
{
public void init()
{
Choice language = new Choice();
language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");
add(language);
  }
}

Get Particular Item of AWT Choice Or Combo box

import java.applet.Applet;
import java.awt.Choice;
import java.awt.Graphics;
public class GetItemExample extends Applet
{
Choice language = null;     
public void init()
{
language = new Choice();
language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");
add(language);             
}     
public void paint(Graphics g)
{
g.drawString(language.getItem(2) + " is at index 2", 10, 70);
   }      
}

Get Selected Item Of AWT Choice Or Combo box

import java.applet.Applet;
import java.awt.Choice;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class GetSelectedItemExample extends Applet implements ItemListener
{
Choice language = null;
public void init()
language = new Choice();
language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");               
add(language);
language.addItemListener(this);             
}     
public void paint(Graphics g)
{
g.drawString(language.getSelectedItem(),10, 70);
}
public void itemStateChanged(ItemEvent arg0)
 {
repaint();             
  }
}

No comments:

Post a Comment