Get Selected Radio Button Example

  1. /*
  2. Get Selected Radio Button Example
  3. This java example shows how to get selected radio button using
  4. Java AWT CheckboxGroup class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Checkbox;
  9. import java.awt.CheckboxGroup;
  10. import java.awt.Graphics;
  11. import java.awt.event.ItemEvent;
  12. import java.awt.event.ItemListener;
  13.  
  14.  
  15. /*
  16. <applet code="GetSelectedRadioButtonExample" width=200 height=200>
  17. </applet>
  18. */
  19.  
  20.  
  21. public class GetSelectedRadioButtonExample extends Applet implements ItemListener{
  22.  
  23. CheckboxGroup lngGrp = null;
  24.  
  25. public void init(){
  26.  
  27. //create group
  28. lngGrp = new CheckboxGroup();
  29.  
  30. //create checkboxes and add to group
  31. Checkbox java = new Checkbox("Java", lngGrp, true);
  32. Checkbox cpp = new Checkbox("C++", lngGrp, false);
  33. Checkbox vb = new Checkbox("VB", lngGrp, false);
  34.  
  35. //add radio buttons
  36. add(java);
  37. add(cpp);
  38. add(vb);
  39.  
  40. //add listeners
  41. java.addItemListener(this);
  42. cpp.addItemListener(this);
  43. vb.addItemListener(this);
  44. }
  45.  
  46.  
  47. public void itemStateChanged(ItemEvent ie) {
  48. repaint();
  49. }
  50.  
  51. public void paint(Graphics g){
  52.  
  53. /*
  54. * To get selected radio button, use
  55. * Checkbox getSelectedCheckbox()
  56. * method of CheckboxGroup class.
  57. */
  58.  
  59. Checkbox chk = lngGrp.getSelectedCheckbox();
  60.  
  61. g.drawString(chk.getLabel() + " is selected", 10 ,70);
  62. }
  63. }

Example Output

Visit Java Example Forums to request Java examples or ask Java questions!

Create your own blog, submit tutorials articles and examples, join community of hundreds of members at Java Community Network




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples