Java Swing - GUIs
TOPIC 03 – MORE ON COMPONENTS

 

LESSON NOTE - JRADIOBUTTON

 

 

JRADIOBUTTON CONSTRUCTOR

 

We create a JRadioButton by specifying the text associated with the radio button.

     

   JRadioButton rbSens = new JRadioButton("Sens");

 

After adding the JRadioButton to the content pane, we get this:

 

 

SELECTED?

 

To know the state of the checkbox, we use the isSelected() method.  It returns either true or false.

 

     boolean state = rbSens.isSelected();

 

SETTING THE STATE

 

To set the state of the checkbox to either selected or not selected, we use the setSelected(boolean) method.

 

     rbSens.setSelected(true);

 

MULTIPLE RADIO BUTTONS

 

Generally, we use several radio buttons together.  And only one radio button can be selected at a time.  However, to do this, we must add all of the radio buttons to a ButtonGroup.  It is the ButtonGroup that will ensure that only one radio button is on at any specific time.

 

     JRadioButton rbSens = new JRadioButton("Sens");

     JRadioButton rbLeafs = new JRadioButton("Leafs");

     JRadioButton rbCanadiens = new JRadioButton("Canadiens");

 

     ButtonGroup group = new ButtonGroup();

     group.add(rbSens);

     group.add(rbLeafs);

     group.add(rbCanadiens);

 

ACTION LISTENER

 

We can add an action listener to each JRadioButton.  After we do so, an event is triggered whenever the radio button is clicked on (even if the radio button was already selected).

 

ACTIONPERFORMED METHOD

 

To respond to an event, we check the source of the event to figure out which radio button triggered the event.

 

   public void actionPerformed(ActionEvent e)

   {

        if(e.getSource() == rbSens)

        {

             System.out.println("Triggered by Sens");

        }

        else if(e.getSource() == rbLeafs)

        {

             System.out.println("Triggered by Leafs");

        }

        else if(e.getSource() == rbCanadiens)

        {

             System.out.println("Triggered by Canadiens");

        }

   }

FULL CODE EXAMPLE

 

Here is a complete example that shows the concepts from above in action.

 

 

public class MyGUI extends JFrame implements ActionListener

{

   //datafields

   JRadioButton rbSens;

   JRadioButton rbLeafs;

   JRadioButton rbCanadiens;

    

   public MyGUI()

   {

        Container cp = this.getContentPane();

        cp.setLayout(new FlowLayout());

           

        rbSens = new JRadioButton("Sens");

        rbSens.setSelected(true);

        rbSens.addActionListener(this);

        cp.add(rbSens);

       

        rbLeafs = new JRadioButton("Leafs");

        rbLeafs.addActionListener(this);

        cp.add(rbLeafs);

       

        rbCanadiens = new JRadioButton("Canadiens");

        rbCanadiens.addActionListener(this);

        cp.add(rbCanadiens);

       

        ButtonGroup group = new ButtonGroup();

        group.add(rbSens);

        group.add(rbLeafs);

        group.add(rbCanadiens);

       

        this.setTitle("JRadioButton");

        this.pack();

        this.setVisible(true);

   }

  

   public void actionPerformed(ActionEvent e)

   {

        if(e.getSource() == rbSens)

        {

             System.out.println("Triggered by Sens");

        }

        else if(e.getSource() == rbLeafs)

        {

             System.out.println("Triggered by Leafs");

        }

        else if(e.getSource() == rbCanadiens)

        {

             System.out.println("Triggered by Canadiens");

        }

   }

    

   public static void main(String[] args)

   {

        MyGUI sg = new MyGUI();

   }

}