The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JRadioButton  [6 examples]

e768. Creating a JRadioButton Component

    // Create an action for each radio button
    Action action1 = new AbstractAction("RadioButton Label1") {
        // This method is called whenever the radio button is pressed,
        // even if it is already selected; this method is not called
        // if the radio button was selected programmatically
        public void actionPerformed(ActionEvent evt) {
            // Perform action
        }
    };
    Action action2 = new AbstractAction("RadioButton Label2") {
        // See above
        public void actionPerformed(ActionEvent evt) {
            // Perform action
        }
    };
    
    // Create the radio buttons using the actions
    JRadioButton b1 = new JRadioButton(action1);
    JRadioButton b2 = new JRadioButton(action2);
    
    // Associate the two buttons with a button group
    ButtonGroup group = new ButtonGroup();
    group.add(b1);
    group.add(b2);
    
    // Neither radio button is selected; to select a radio button,
    // see e769 Selecting a JRadioButton Component in a Button Group

 Related Examples
e769. Selecting a JRadioButton Component in a Button Group
e770. Determining the Selected JRadioButton in a Button Group
e771. Determining If a JRadioButton Component Is Selected
e772. Adding an Icon to the Label of a JRadioButton Component
e773. Customizing the Icons in a JRadioButton Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.