Results 1 to 2 of 2
  1. #1
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default JRadioButton Logics

    I want to make a logic in which only oe Radio Button is selected at a time and othe deselects.
    i'm using this code as an example

    Java Code:
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.*;
    
    
    public class Radio implements ItemListener{
    
    	JFrame f;
    	JRadioButton b1,b2,b3;
    	
    Radio(){
    	
    	ButtonGroup b= new ButtonGroup();
    	b.add(b1);
    	b.add(b2);
    	b.add(b3); 
    	
    	f= new JFrame("Frame");
    	b1= new JRadioButton("A");
    	b1.setLocation(10,20);
    	b1.setSize(b1.getPreferredSize());
    	b1.addItemListener(this);
    	
    	b2= new JRadioButton("B");
    	b2.setLocation(30,40);
    	b2.setSize(b2.getPreferredSize());
    	b2.addItemListener(this);
    	b3= new JRadioButton("C");                        
    	b3.setLocation(60,60);
    	b3.setSize(b3.getPreferredSize());
    	b3.addItemListener(this);
    
    	
    	f.setSize(400,200);
    	f.setLayout(null);
    	f.add(b1);	
    	f.add(b2);
    	f.add(b3);
    	
    	f.setVisible(true);
    	
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new Radio();
    	}
    	@Override 
    	public void itemStateChanged(ItemEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getSource()==b1){
    			//b1.setSelected(true);
    			b2.setSelected(false);
    			b3.setSelected(false);
    			
    		}
    		if(e.getSource()==b2){
    			//b2.setSelected(true);
    			b1.setSelected(false);
    			b3.setSelected(false);
    			
    		}
    		if(e.getSource()==b3){
    			//b3.setSelected(true);
    			b1.setSelected(false);
    			b2.setSelected(false);
    }
    }
    }
    The problem is that after using setSelected when i select othe RAdioButton it is'nt selected on one click.

    Need help here

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: JRadioButton Logics

    Try doing some debugging by adding a println() statement in the itemStateChanged() method to see when it is called and what the value of ItemEvent is.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. JRadioButton
    By gyijhbk in forum New To Java
    Replies: 3
    Last Post: 06-13-2012, 09:26 PM
  2. Where tu put the logics
    By Marco_2000_2011 in forum Advanced Java
    Replies: 0
    Last Post: 10-04-2011, 04:33 PM
  3. replacing last char of string logics
    By mukeshgulia in forum New To Java
    Replies: 3
    Last Post: 03-25-2011, 09:30 AM
  4. Help on using JRadioButton with images
    By Nych in forum New To Java
    Replies: 3
    Last Post: 03-16-2011, 08:12 AM
  5. JRadioButton
    By mcajavaprogramer in forum AWT / Swing
    Replies: 1
    Last Post: 11-05-2010, 01:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •