Pages

Thursday, September 8, 2016

create radio button java

Explanation

In java programming some times you have multiple choice in something and you have to select true of them or false of them..so to do that here comes JRadioButton....JRadioButton help you in choosing something in java.So in this page i am going to show you how to create and add them into Java JFrame or JPanel
package myPackage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class myClass extends JFrame 
             implements ActionListener{
   
  /*create JPanel*/
  JPanel panel = new JPanel();
  
   
     /*create RadioButtons here*/
  
 JRadioButton button =new JRadioButton();
 JRadioButton button1=new JRadioButton();
 JRadioButton button2=new JRadioButton();
 JRadioButton button3=new JRadioButton();
  
 public myClass(){
   setSize(400,600);
  setDefaultCloseOperation
  (JFrame.EXIT_ON_CLOSE);
     setVisible(true);
  setTitle("Radio Button");
  add(panel);
  radioButton();
 
 }
    
 
 public void radioButton(){
  
   
     /*add all buttons to 
      * the panel */
 
     panel.add(button);
     panel.add(button1);   
     panel.add(button2);
     panel.add(button3);
    
  
     /*add text on your button*/ 
     button.setText("apple");
     button1.setText("guava");
     button2.setText("banana");
     button3.setText("orange");
  
 
     /*select them false*/
     button.setSelected(false);
     button1.setSelected(false);
     button2.setSelected(false);
     button3.setSelected(false);
  
   
     /*now add actionListener*/
     button.addActionListener(this);
     button1.addActionListener(this);
     button2.addActionListener(this);
     button3.addActionListener(this);
  
  
  
 }
     
  
/*method for to add actionPerformed*/
 
public void actionPerformed(ActionEvent e){
     Object source = e.getSource();
     
    
 /*actionListener for button*/
     
 if(source==button ){
    
     button.setSelected(true);
     button1.setSelected(false);
     button2.setSelected(false);
     button3.setSelected(false);
 
  
    /*only button to be true*/
 }
 
  
     /*actionListener for button1 */ 
      
 if(source==button1  ){
      
     button.setSelected(false);
     button1.setSelected(true);
     button2.setSelected(false);
     button3.setSelected(false);
   
    
 /*only button1 to be true*/
   }
 
 
 
  /*actionListener for button2*/ 

 if(source==button2){
 
     button.setSelected(false);
     button1.setSelected(false);
     button2.setSelected(true);
     button3.setSelected(false);
   
   
 /*only button2 to be true*/
   }
  
  
   
    /*actionListener for button3*/ 

  if(source==button3){
      
     button.setSelected(false);
     button1.setSelected(false);
     button2.setSelected(false);
     button3.setSelected(true);
   
    
    /*only button3 to be true*/
   }
 }
}
And now if your run this so it would like below

No comments:

Post a Comment