i am havin a problem using a RadioButton to control labels on a panel.Anytime i click on one of the JRadio button it controls all the other panels at the same time instead of only controlling its label on its panel.

Please all hw do i go about setting my radio button to only control the label on its own panel and not all the labels on the other panels?

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

import javax.swing.ButtonGroup;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;


public class radioButon extends JInternalFrame{
	
	//JPanel
	private JPanel jContentPane = null;
	
	//JRadioButton
	private JRadioButton Radio_1 = null;
	private JRadioButton  Radio_2 = null;
	private JRadioButton  Radio_3 = null;
	private ButtonGroup BttnGroupUpdType = new ButtonGroup();
	
	//JLabel
	private JLabel jlblFile = null;
	
	public radioButon(){
		super("Radio Button", false, 
				true,  //closable 
				false, //icon 
				true); //minimizeable
		this.setName("Radio Button");
		initialize();
	}
	
	private void initialize(){
		this.setSize(310, 230);
		this.setTitle("Radio Button");
		this.setResizable(false);
		this.setVisible(true);
		this.setContentPane(getJContentPane());
		this.addInternalFrameListener(new InternalFrameListener(){
			public void internalFrameActivated(InternalFrameEvent arg0) {
			}
			public void internalFrameClosed(InternalFrameEvent arg0) {}
			public void internalFrameClosing(InternalFrameEvent arg0) {
			}
			public void internalFrameDeactivated(InternalFrameEvent arg0) {
			}
			public void internalFrameDeiconified(InternalFrameEvent arg0) {}
			public void internalFrameIconified(InternalFrameEvent arg0) {}
			public void internalFrameOpened(InternalFrameEvent arg0) {}
		});
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			
			jlblFile = new JLabel();
			jlblFile.setBounds(7, 100, 300, 16);
			jlblFile.setText("Click any Radio Button! ^_^");
			jlblFile.setFont(new Font("Tahoma", Font.BOLD, 12));
			jlblFile.grabFocus();
			jlblFile.setVisible(true);
			
		}
		jContentPane = new JPanel();
		jContentPane.setLayout(null);
		
		jContentPane.add(jlblFile,null);
		jContentPane.add(getRadio_1(), null);
		jContentPane.add(getRadio_2(), null);
		jContentPane.add(getRadio_3(), null);
		
		BttnGroupUpdType.add(getRadio_1());
		BttnGroupUpdType.add(getRadio_2());
		BttnGroupUpdType.add(getRadio_3());
		getButtonUpdType();
		
		return jContentPane;
	}
	
	private ButtonGroup getButtonUpdType(){
		BttnGroupUpdType = new ButtonGroup();
		BttnGroupUpdType.add(Radio_1);
		BttnGroupUpdType.add(Radio_2);
		BttnGroupUpdType.add(Radio_3);
		return BttnGroupUpdType;
	}
	
	private JRadioButton getRadio_1() {
		if (Radio_1 == null) {
			Radio_1 = new JRadioButton();
			Radio_1.setBounds(new Rectangle(7, 10, 200, 16));
			Radio_1.setFont(new Font("Tahoma", Font.BOLD, 12));
			Radio_1.setText("Radio_1");
			Radio_1.setSelected(false);
			Radio_1.setEnabled(true);
			Radio_1.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					try {
						jlblFile.setText("YOU CLICK RADIO _ 1");
					}catch (Exception e) {
						e.printStackTrace();
					}
				}				
			});
		}
		return Radio_1;
	}
	
	private JRadioButton getRadio_2() {
		if (Radio_2 == null) {
			Radio_2 = new JRadioButton();
			Radio_2.setBounds(new Rectangle(7, 30, 200, 16));
			Radio_2.setFont(new Font("Tahoma", Font.BOLD, 12));
			Radio_2.setText("Radio_2");
			Radio_2.setSelected(false);
			Radio_2.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					try{
						jlblFile.setText("What will happen!! ^_^\n"+" YOU CLICK RADIO _ 2");
					}catch (Exception e) {
						// TODO: handle exception
					}
				}				
			});
		}
	  	return Radio_2;
	  }	
	
	private JRadioButton getRadio_3() {
		if (Radio_3 == null) {
			Radio_3 = new JRadioButton();
			Radio_3.setBounds(new Rectangle(7, 50, 200, 16));
			Radio_3.setFont(new Font("Tahoma", Font.BOLD, 12));
			Radio_3.setText("Radio_3");
			Radio_3.setSelected(false);
			Radio_3.setEnabled(true);
			Radio_3.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					try {
						jlblFile.setText("see!! ^_^\n"+" YOU CLICK RADIO _ 3");
					}catch (Exception e) {
						e.printStackTrace();
					}
				}				
			});
		}
	  	return Radio_3;
	  }
}

USE

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

import javax.swing.ButtonGroup;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;


public class radioButon extends JInternalFrame{

    //JPanel
    private JPanel jContentPane = null;

    //JRadioButton
    private JRadioButton Radio_1 = null;
    private JRadioButton  Radio_2 = null;
    private JRadioButton  Radio_3 = null;
    private ButtonGroup BttnGroupUpdType = new ButtonGroup();

    //JLabel
    private JLabel jlblFile = null;

    public radioButon(){
        super("Radio Button", false, 
                true,  //closable 
                false, //icon 
                true); //minimizeable
        this.setName("Radio Button");
        initialize();
    }

    private void initialize(){
        this.setSize(310, 230);
        this.setTitle("Radio Button");
        this.setResizable(false);
        this.setVisible(true);
        this.setContentPane(getJContentPane());
        this.addInternalFrameListener(new InternalFrameListener(){
            public void internalFrameActivated(InternalFrameEvent arg0) {
            }
            public void internalFrameClosed(InternalFrameEvent arg0) {}
            public void internalFrameClosing(InternalFrameEvent arg0) {
            }
            public void internalFrameDeactivated(InternalFrameEvent arg0) {
            }
            public void internalFrameDeiconified(InternalFrameEvent arg0) {}
            public void internalFrameIconified(InternalFrameEvent arg0) {}
            public void internalFrameOpened(InternalFrameEvent arg0) {}
        });
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {

            jlblFile = new JLabel();
            jlblFile.setBounds(7, 100, 300, 16);
            jlblFile.setText("Click any Radio Button! ^_^");
            jlblFile.setFont(new Font("Tahoma", Font.BOLD, 12));
            jlblFile.grabFocus();
            jlblFile.setVisible(true);

        }
        jContentPane = new JPanel();
        jContentPane.setLayout(null);

        jContentPane.add(jlblFile,null);
        jContentPane.add(getRadio_1(), null);
        jContentPane.add(getRadio_2(), null);
        jContentPane.add(getRadio_3(), null);

        BttnGroupUpdType.add(getRadio_1());
        BttnGroupUpdType.add(getRadio_2());
        BttnGroupUpdType.add(getRadio_3());
        getButtonUpdType();

        return jContentPane;
    }

    private ButtonGroup getButtonUpdType(){
        BttnGroupUpdType = new ButtonGroup();
        BttnGroupUpdType.add(Radio_1);
        BttnGroupUpdType.add(Radio_2);
        BttnGroupUpdType.add(Radio_3);
        return BttnGroupUpdType;
    }

    private JRadioButton getRadio_1() {
        if (Radio_1 == null) {
            Radio_1 = new JRadioButton();
            Radio_1.setBounds(new Rectangle(7, 10, 200, 16));
            Radio_1.setFont(new Font("Tahoma", Font.BOLD, 12));
            Radio_1.setText("Radio_1");
            Radio_1.setSelected(false);
            Radio_1.setEnabled(true);
            Radio_1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                    try {
                        jlblFile.setText("YOU CLICK RADIO _ 1");
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }               
            });
        }
        return Radio_1;
    }

    private JRadioButton getRadio_2() {
        if (Radio_2 == null) {
            Radio_2 = new JRadioButton();
            Radio_2.setBounds(new Rectangle(7, 30, 200, 16));
            Radio_2.setFont(new Font("Tahoma", Font.BOLD, 12));
            Radio_2.setText("Radio_2");
            Radio_2.setSelected(false);
            Radio_2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                    try{
                        jlblFile.setText("What will happen!! ^_^\n"+" YOU CLICK RADIO _ 2");
                    }catch (Exception e) {
                        // TODO: handle exception
                    }
                }               
            });
        }
        return Radio_2;
      } 

    private JRadioButton getRadio_3() {
        if (Radio_3 == null) {
            Radio_3 = new JRadioButton();
            Radio_3.setBounds(new Rectangle(7, 50, 200, 16));
            Radio_3.setFont(new Font("Tahoma", Font.BOLD, 12));
            Radio_3.setText("Radio_3");
            Radio_3.setSelected(false);
            Radio_3.setEnabled(true);
            Radio_3.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                    try {
                        jlblFile.setText("see!! ^_^\n"+" YOU CLICK RADIO _ 3");
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }               
            });
        }
        return Radio_3;
      }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.