JaVa
   

The JRadioButton

The JRadioButton acts in a similar way to a JCheckBox, except we need to use the ButtonGroup class to "group" the radio buttons so that only one can be selected in a group. Note that the ButtonGroup is a logical grouping and not a physical one, so even though we add the JRadioButtons to a button group, we still need to add them to the content pane. Let's create an example app that has two groups of three radio buttons and two labels to tell us which one is selected in each of the two groups. Here is the complete code listing for the example:

Code Listing 11: Using the JRadioButton
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JRadioButtonExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 JRadioButtonExample mainApp = new JRadioButtonExample();
 }
 public JRadioButtonExample()
 {
 super("JRadioButton Example");
 setBounds(0, 0, 300, 300);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the labels...
 label1 = new JLabel("Group 1");
 label1.setLocation(10, 10);
 label1.setSize(label1.getPreferredSize());
 label2 = new JLabel("Group 2");
 label2.setLocation(150, 10);
 label2.setSize(label2.getPreferredSize());
 label3 = new JLabel("Group 1 has JRadioButton 1 selected");
 label3.setLocation(10, 200);
 label3.setSize(label3.getPreferredSize());
 label4 = new JLabel("Group 2 has JRadioButton 4 selected");
 label4.setLocation(10, 220);
 label4.setSize(label4.getPreferredSize());
 // Create the six radio buttons...
 radiobutton1 = new JRadioButton("Radio Button 1", true);
 radiobutton1.setLocation(10, 40);
 radiobutton1.setSize(radiobutton1.getPreferredSize());
 radiobutton2 = new JRadioButton("Radio Button 2", false);
 radiobutton2.setLocation(10, 60);
 radiobutton2.setSize(radiobutton2.getPreferredSize());
 radiobutton3 = new JRadioButton("Radio Button 3", false);
 radiobutton3.setLocation(10, 80);
 radiobutton3.setSize(radiobutton3.getPreferredSize());
 radiobutton4 = new JRadioButton("Radio Button 4", true);
 radiobutton4.setLocation(150, 40);
 radiobutton4.setSize(radiobutton1.getPreferredSize());
 radiobutton5 = new JRadioButton("Radio Button 5", false);
 radiobutton5.setLocation(150, 60);
 radiobutton5.setSize(radiobutton2.getPreferredSize());
 radiobutton6 = new JRadioButton("Radio Button 6", false);
 radiobutton6.setLocation(150, 80);
 radiobutton6.setSize(radiobutton3.getPreferredSize());
 // Assign the JRadioButtons to ButtonGroups
 group1 = new ButtonGroup();
 group1.add(radiobutton1);
 group1.add(radiobutton2);
 group1.add(radiobutton3);
 group2 = new ButtonGroup();
 group2.add(radiobutton4);
 group2.add(radiobutton5);
 group2.add(radiobutton6);
 // Add the action listeners
 radiobutton1.addActionListener(this);
 radiobutton2.addActionListener(this);
 radiobutton3.addActionListener(this);
 radiobutton4.addActionListener(this);
 radiobutton5.addActionListener(this);
 radiobutton6.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(label1);
 getContentPane().add(label2);
 getContentPane().add(label3);
 getContentPane().add(label4);
 getContentPane().add(radiobutton1);
 getContentPane().add(radiobutton2);
 getContentPane().add(radiobutton3);
 getContentPane().add(radiobutton4);
 getContentPane().add(radiobutton5);
 getContentPane().add(radiobutton6);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == radiobutton1 ||
 e.getSource() == radiobutton2 ||
 e.getSource() == radiobutton3 ||
 e.getSource() == radiobutton4 ||
 e.getSource() == radiobutton5 ||
 e.getSource() == radiobutton6)
 { if(radiobutton1.isSelected())
 {
 label3.setText("Group 1 has JRadioButton 1
 selected");
 label3.setSize(label3.getPreferredSize()); }
 else if(radiobutton2.isSelected())
 {
 label3.setText("Group 1 has JRadioButton 2
 selected");
 label3.setSize(label3.getPreferredSize()); }
 else if(radiobutton3.isSelected())
 {
 label3.setText("Group 1 has JRadioButton 3
 selected");
 label3.setSize(label3.getPreferredSize()); }
 if(radiobutton4.isSelected())
 {
 label4.setText("Group 2 has JRadioButton 4
 selected");
 label4.setSize(label4.getPreferredSize()); }
 else if(radiobutton5.isSelected())
 {
 label4.setText("Group 2 has JRadioButton 5
 selected");
 label4.setSize(label4.getPreferredSize()); }
 else if(radiobutton6.isSelected())
 {
 label4.setText("Group 2 has JRadioButton 6
 selected");
 label4.setSize(label4.getPreferredSize()); }
 }
 }
 JLabel label1;
 JLabel label2;
 JLabel label3;
 JLabel label4;
 JRadioButton radiobutton1;
 JRadioButton radiobutton2;
 JRadioButton radiobutton3;
 JRadioButton radiobutton4;
 JRadioButton radiobutton5;
 JRadioButton radiobutton6;
 ButtonGroup group1;
 ButtonGroup group2;
}


Java End example

If we now execute this example app, the following app frame can be seen:

Java ScreenShot
Screenshot: Using the JRadioButton

If we now change the selection in both of the groups, we can see that if, for example, we change the selection in group 1 from Radio Button 1 to Radio Button 3, Radio Button 1 is deselected and Radio Button 3 is selected instead. Note how Group 2 is unaffected, as it is not connected in any way to Group 1. We can see the selections are retrieved and displayed in the two labels below the radio buttons as we press them. This can be seen in the following screen shot:

Java ScreenShot
Screenshot: This shows the selection of the radio buttons being changed.

Let's now take a look at the code that we used to implement the JRadioButtons. First we create the JRadioButtons in the same way that we created the JCheckButtons (using the contructor to specify the text to appear to the right of the radio button and also its initial state). Here is a sample of one of the code segments used to create a single JRadioButton:

radiobutton1 = new JRadioButton("Radio Button 1", true);
radiobutton1.setLocation(10, 40);
radiobutton1.setSize(radiobutton1.getPreferredSize());


Once we have created six JRadioButtons, we then create two ButtonGroup objects. For each of these two objects, we use the add method to add three JRadioButtons to each of the groups. This assigns the logical grouping for our radio buttons, ensuring that only one option in each of the groups can be selected at any one time. Here is the code segment that we have used to accomplish this:

group1 = new ButtonGroup();
group1.add(radiobutton1);
group1.add(radiobutton2);
group1.add(radiobutton3);
group2 = new ButtonGroup();
group2.add(radiobutton4);
group2.add(radiobutton5);
group2.add(radiobutton6);


As we said before, the ButtonGroup object is just a logical grouping and does not, therefore, add them to the content pane. We add the JRadioButtons to the content pane using the following six lines of code, one for each of the buttons:

getContentPane().add(radiobutton1);
getContentPane().add(radiobutton2);
getContentPane().add(radiobutton3);
getContentPane().add(radiobutton4);
getContentPane().add(radiobutton5);
getContentPane().add(radiobutton6);


To find which radio button is selected in each of the groups, we use the same isSelected method that we used for the JCheckBox.

Creating Image Radio Buttons

Let's look at how we can add our own images to represent the three states of the radio buttons rather than use the default GUI. We will look at a complete example that is a modification of the previous JRadioButton example code.

Code Listing 12: Image radio buttons
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageRadioButtonExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 ImageRadioButtonExample mainApp = new
 ImageRadioButtonExample();
 }
 public ImageRadioButtonExample()
 {
 super("Image Radio Button Example");
 setBounds(0, 0, 300, 300);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the labels...
 label1 = new JLabel("Group 1");
 label1.setLocation(10, 10);
 label1.setSize(label1.getPreferredSize());
 label2 = new JLabel("Group 2");
 label2.setLocation(150, 10);
 label2.setSize(label2.getPreferredSize());
 label3 = new JLabel("Group 1 has JRadioButton 1 selected");
 label3.setLocation(10, 200);
 label3.setSize(label3.getPreferredSize());
 label4 = new JLabel("Group 2 has JRadioButton 4 selected");
 label4.setLocation(10, 220);
 label4.setSize(label4.getPreferredSize());
 // Create the six radio buttons...
 radiobutton1 = new JRadioButton("Radio Button 1", new
 ImageIcon("off.gif"));
 radiobutton1.setRolloverIcon(new ImageIcon("over.gif"));
 radiobutton1.setSelectedIcon(new ImageIcon("on.gif"));
 radiobutton1.setRolloverSelectedIcon(new
 ImageIcon("onandover.gif"));
 radiobutton1.setFocusPainted(false);
 radiobutton1.setBorderPainted(false);
 radiobutton1.setContentAreaFilled(false);
 radiobutton1.setLocation(10, 40);
 radiobutton1.setSize(radiobutton1.getPreferredSize());
 radiobutton2 = new JRadioButton("Radio Button 2", new
 ImageIcon("off.gif"));
 radiobutton2.setRolloverIcon(new ImageIcon("over.gif"));
 radiobutton2.setSelectedIcon(new ImageIcon("on.gif"));
 radiobutton2.setRolloverSelectedIcon(new
 ImageIcon("onandover.gif"));
 radiobutton2.setFocusPainted(false);
 radiobutton2.setBorderPainted(false);
 radiobutton2.setContentAreaFilled(false);
 radiobutton2.setLocation(10, 60);
 radiobutton2.setSize(radiobutton2.getPreferredSize());
 radiobutton3 = new JRadioButton("Radio Button 3", new
 ImageIcon("off.gif"));
 radiobutton3.setRolloverIcon(new ImageIcon("over.gif"));
 radiobutton3.setSelectedIcon(new ImageIcon("on.gif"));
 radiobutton3.setRolloverSelectedIcon(new
 ImageIcon("onandover.gif"));
 radiobutton3.setFocusPainted(false);
 radiobutton3.setBorderPainted(false);
 radiobutton3.setContentAreaFilled(false);
 radiobutton3.setLocation(10, 80);
 radiobutton3.setSize(radiobutton3.getPreferredSize());
 radiobutton4 = new JRadioButton("Radio Button 4", new
 ImageIcon("off.gif"));
 radiobutton4.setRolloverIcon(new ImageIcon("over.gif"));
 radiobutton4.setSelectedIcon(new ImageIcon("on.gif"));
 radiobutton4.setRolloverSelectedIcon(new
 ImageIcon("onandover.gif"));
 radiobutton4.setFocusPainted(false);
 radiobutton4.setBorderPainted(false);
 radiobutton4.setContentAreaFilled(false);
 radiobutton4.setLocation(150, 40);
 radiobutton4.setSize(radiobutton1.getPreferredSize());
 radiobutton5 = new JRadioButton("Radio Button 5", new
 ImageIcon("off.gif"));
 radiobutton5.setRolloverIcon(new ImageIcon("over.gif"));
 radiobutton5.setSelectedIcon(new ImageIcon("on.gif"));
 radiobutton5.setRolloverSelectedIcon(new
 ImageIcon("onandover.gif"));
 radiobutton5.setFocusPainted(false);
 radiobutton5.setBorderPainted(false);
 radiobutton5.setContentAreaFilled(false);
 radiobutton5.setLocation(150, 60);
 radiobutton5.setSize(radiobutton2.getPreferredSize());
 radiobutton6 = new JRadioButton("Radio Button 6", new
 ImageIcon("off.gif"));
 radiobutton6.setRolloverIcon(new ImageIcon("over.gif"));
 radiobutton6.setSelectedIcon(new ImageIcon("on.gif"));
 radiobutton6.setRolloverSelectedIcon(new
 ImageIcon("onandover.gif"));
 radiobutton6.setFocusPainted(false);
 radiobutton6.setBorderPainted(false);
 radiobutton6.setContentAreaFilled(false);
 radiobutton6.setLocation(150, 80);
 radiobutton6.setSize(radiobutton3.getPreferredSize());
 // Assign the JRadioButtons to ButtonGroups
 group1 = new ButtonGroup();
 group1.add(radiobutton1);
 group1.add(radiobutton2);
 group1.add(radiobutton3);
 group2 = new ButtonGroup();
 group2.add(radiobutton4);
 group2.add(radiobutton5);
 group2.add(radiobutton6);
 // Add the action listeners
 radiobutton1.addActionListener(this);
 radiobutton2.addActionListener(this);
 radiobutton3.addActionListener(this);
 radiobutton4.addActionListener(this);
 radiobutton5.addActionListener(this);
 radiobutton6.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(label1);
 getContentPane().add(label2);
 getContentPane().add(label3);
 getContentPane().add(label4);
 getContentPane().add(radiobutton1);
 getContentPane().add(radiobutton2);
 getContentPane().add(radiobutton3);
 getContentPane().add(radiobutton4);
 getContentPane().add(radiobutton5);
 getContentPane().add(radiobutton6);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == radiobutton1 ||
 e.getSource() == radiobutton2 ||
 e.getSource() == radiobutton3 ||
 e.getSource() == radiobutton4 ||
 e.getSource() == radiobutton5 ||
 e.getSource() == radiobutton6)
 { if(radiobutton1.isSelected())
 {
 label3.setText("Group 1 has JRadioButton 1
 selected");
 label3.setSize(label3.getPreferredSize()); }
 else if(radiobutton2.isSelected())
 {
 label3.setText("Group 1 has JRadioButton 2
 selected");
 label3.setSize(label3.getPreferredSize()); }
 else if(radiobutton3.isSelected())
 {
 label3.setText("Group 1 has JRadioButton 3
 selected");
 label3.setSize(label3.getPreferredSize()); }
 if(radiobutton4.isSelected())
 {
 label4.setText("Group 2 has JRadioButton 4
 selected");
 label4.setSize(label4.getPreferredSize()); }
 else if(radiobutton5.isSelected())
 {
 label4.setText("Group 2 has JRadioButton 5
 selected");
 label4.setSize(label4.getPreferredSize()); }
 else if(radiobutton6.isSelected())
 {
 label4.setText("Group 2 has JRadioButton 6
 selected");
 label4.setSize(label4.getPreferredSize()); }
 }
 }
 JLabel label1;
 JLabel label2;
 JLabel label3;
 JLabel label4;
 JRadioButton radiobutton1;
 JRadioButton radiobutton2;
 JRadioButton radiobutton3;
 JRadioButton radiobutton4;
 JRadioButton radiobutton5;
 JRadioButton radiobutton6;
 ButtonGroup group1;
 ButtonGroup group2;
}



Java End example

When we run the new example with the images that can be found on this DVD in the same directory as the code, you can see that the radio buttons now have their own custom graphics for each of the different possible states. Here is a screen shot of the example app:

Java ScreenShot
Screenshot: Replacing the standard JRadioButtons with custom images

As you can see, using you own graphics for radio buttons is very simple and is done in much the same way as JButtons and JCheckBoxes. Let's look at the code that we used to create one of the individual radio buttons. The first thing we have changed is the JRadioButton constructor, which now takes in an icon and the text that is to appear to the right of the radio button. This can be seen in the following line of code:

radiobutton1 = new JRadioButton("Radio Button 1", new
 ImageIcon("off.gif"));


The image that we specify in the constructor represents the default image of the radio button (i.e., the image that is to be displayed if no other images are relevant). After we have specified the default image, we then specify the images for when the mouse rolls over the radio button, selects it, and rolls over it while it is selected. This can be seen in the following few lines of code:

radiobutton1.setRolloverIcon(new ImageIcon("over.gif"));
radiobutton1.setSelectedIcon(new ImageIcon("on.gif"));
radiobutton1.setRolloverSelectedIcon(new ImageIcon("onandover.gif"));


Note that there are also two other methods available that we have not used in this example; the first is called setPressedIcon, which allows you to specify the image that should be displayed while the mouse button is held down over the radio button, and the second is setDisabledIcon, which is displayed if the radio button has been disabled. Next we need to remove the default decorations that are applied to the radio button the same way as we did for the JButton and the JCheckBox. This is accomplished with the final following three lines of code:

radiobutton1.setFocusPainted(false);
radiobutton1.setBorderPainted(false);
radiobutton1.setContentAreaFilled(false);


Apart from the other five buttons that have been modified in the same way as this one to show the custom images, the rest of the example is exactly the same.

JaVa
   
Comments