Monday, 13 April 2015

ButtonGroup class

ButtonGroup class:

The ButtonGroup class can be used to group multiple buttons so that at a time only one button can be selected.

JRadioButton example with event handling

1.    import javax.swing.*;  
2.    import java.awt.event.*;  
3.    class RadioExample extends JFrame implements ActionListener{  
4.    JRadioButton rb1,rb2;  
5.    JButton b;  
6.    RadioExample(){  
7.      
8.    rb1=new JRadioButton("Male");  
9.    rb1.setBounds(100,50,100,30);  
10.   
11. rb2=new JRadioButton("Female");  
12. rb2.setBounds(100,100,100,30);  
13.   
14. ButtonGroup bg=new ButtonGroup();  
15. bg.add(rb1);bg.add(rb2);  
16.   
17. b=new JButton("click");  
18. b.setBounds(100,150,80,30);  
19. b.addActionListener(this);  
20.   
21. add(rb1);add(rb2);add(b);  
22.   
23. setSize(300,300);  
24. setLayout(null);  
25. setVisible(true);  
26. }  
27. public void actionPerformed(ActionEvent e){  
28. if(rb1.isSelected()){  
29. JOptionPane.showMessageDialog(this,"You are male");  
30. }  
31. if(rb2.isSelected()){  
32. JOptionPane.showMessageDialog(this,"You are female");  
33. }  
34. }  
35. public static void main(String args[]){  
36. new RadioExample();  

37. }}  







No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...