INTRODUCTION OF JRADIOBUTTON

Introduction of JRadioButton


JRadioButton Constructor

    JRadioButton

    JRadioButton is similar to JCheckbox, except for the default icon for each class. A set of radio buttons can be associated as a group in which only one button at a time can be selected.

    JRadioButton()

    Creates an initially unselected radio button with no set text.

    JRadioButton(Action a)

    Creates a radiobutton where properties are taken from the Action supplied.

    JRadioButton(Icon icon)

    Creates an initially unselected radio button with the specified image but no text.

    JRadioButton(Icon icon, boolean selected)

    Creates a radio button with the specified image and selection state, but no text.

    JRadioButton(String text)

    Creates an unselected radio button with the specified text.

    JRadioButton(String text, boolean selected)

    Creates a radio button with the specified text and selection state..

    JRadioButton(String text, Icon icon)

    Creates a radio button that has the specified text and image, and that is initially unselected.

    JRadioButton(String text, Icon icon, boolean selected)

    Creates a radio button that has the specified text, image, and selection state.

Example:JRadio Button Constructor.

 
            
                package com.swing.sdj;
                import java.awt.BorderLayout;
                import java.awt.Dimension;
                import java.awt.GridLayout;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.awt.event.KeyEvent;
                import javax.swing.BorderFactory;
                import javax.swing.ButtonGroup;
                import javax.swing.ImageIcon;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                import javax.swing.JPanel;
                import javax.swing.JRadioButton;
                public class JrdButtonDemo extends JPanel
                {
                    static JFrame frame;
                    JLabel jlbPicture;
                    RadioListener myListener = null;
                    public JrdButtonDemo()
                    {
                        /* Create the radio buttons and assign Keyboard shortcuts using */
                        /* Mnemonics */
                      
                        JRadioButton jrbNumbers = new JRadioButton("J2SE");
                        jrbNumbers.setMnemonic(KeyEvent.VK_N);
                        jrbNumbers.setActionCommand("J2SE");
                        jrbNumbers.setSelected(true);
                        JRadioButton jrbAlphabets = new JRadioButton("J2EE");
                        jrbAlphabets.setMnemonic(KeyEvent.VK_A);
                        jrbAlphabets.setActionCommand("J2EE");
                        JRadioButton jrbSymbols = new JRadioButton("FRAMEWORK");
                        jrbSymbols.setMnemonic(KeyEvent.VK_S);
                        jrbSymbols.setActionCommand("FRAMEWORK");

                        /* Group the radio buttons. */
                        ButtonGroup group = new ButtonGroup();
                        group.add(jrbNumbers);
                        group.add(jrbAlphabets);
                        group.add(jrbSymbols);

                        /* Register an action listener for the radio buttons. */
                        myListener = new RadioListener();
                        jrbNumbers.addActionListener(myListener);
                        jrbAlphabets.addActionListener(myListener);
                        jrbSymbols.addActionListener(myListener);

                        /* Set up the picture label */
                        jlbPicture = new JLabel(new ImageIcon("" + "D:\sdj" + ".jpg"));

                        /* Set the Default Image */
                        jlbPicture.setPreferredSize(new Dimension(177, 122));

                        /* Put the radio buttons in a column in a panel */
                        JPanel jplRadio = new JPanel();
                        jplRadio.setLayout(new GridLayout(0, 1));
                        jplRadio.add(jrbNumbers);
                        jplRadio.add(jrbAlphabets);
                        jplRadio.add(jrbSymbols);
                        setLayout(new BorderLayout());
                        add(jplRadio, BorderLayout.WEST);
                        add(jlbPicture, BorderLayout.CENTER);
                        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
                    }
                    /*  Listens to the radio buttons. */
                    class RadioListener implements ActionListener
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            System.out.println(e.getActionCommand().toLowerCase());
                            jlbPicture.setIcon(new ImageIcon("D:\" + e.getActionCommand()+ ".jpg"));
                        }
                    }
                    public static void main(String s[])
                    {
                        frame = new JFrame("JRadioButton Usage Demo");
                        frame.getContentPane().add(new JrdButtonDemo(), BorderLayout.CENTER);
                        frame.pack();
                        frame.setVisible(true);
                    }
                } 
                

Output:

Naming Convention
SDJ Radio Button Demo
Naming Convention
SDJ Radio Button Demo
Naming Convention
SDJ Radio Button Demo