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 Selecting a JRadioButton Component in a Button Group

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.