How to add radio buttons in java
Adding Radio Buttons
Firstly add the radio buttons to the JFrame and set accordingly.
Now run your program and you will notice that you are able to select all the buttons added to JFrame. This was the mistake I discussing above.
So, to remove this discrepancy you just have to include the ButtonGroup from the Pallete.
And change the button group property of the radio buttons by selecting the ButtonGroup you have dragged earlier.
Now it will be working smoothly.
Now let's see how this can be done programmatically.
ButtonGroup buttonGroup1 = new ButtonGroup(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); jRadioButton1.setText("Male"); jRadioButton2.setText("Female"); buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2);
And you will get the same result.
Latest posts by Amit Rawat (see all)
- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019