Pages

Sunday, November 8, 2015

how to create JRadiobuttons in java with whole set up java beginner

Assalam-o-alaikum warahmat-ullahi-wabarakaatuhu.

      

                                           HOW TO CREATE JRADIOBUTTONS WITH WHOLE SITTING
                        IN JAVA 




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 RadioButton here*/

 JRadioButton button =new JRadioButton("apple");
 JRadioButton button1 =new JRadioButton("banana");
 JRadioButton button2 =new JRadioButton("guava");
 JRadioButton button3 =new JRadioButton("mangoes");

 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);

/*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 if you run this so it would look like




No comments:

Post a Comment