Java

Java JRadioButton with ActionListener Example using Netbeans

Java JRadioButton with Example:

Java JRadioButton with ActionListener Example using Netbeans- The JRadioButton class is used to create so-called radio buttons. Radio buttons are shown as circles that fill in when selected. The difference in usage of the checkbox is that radio buttons are usually used when only exactly one option can be selected. To ensure this, the JRadioButtons are usually grouped together in a ButtonGroup.


Java JRadioButton Constructors:

In addition to the standard constructor, the JRadioButton class has a few other constructors that we would like to explain in more detail in the following table:

Constructor Description
JRadioButton (Action a) Here, a Java JRadioButton is directly assigned an action that specifies how the button should react when it is pressed.
JRadioButton (Icon icon) This constructor creates a Java JRadioButton object with an Icon.
JRadioButton (String text) Here a Java JRadioButton is created with a text as a label.
JRadioButton (String text, Icon icon) Using this constructor, both a text and an icon can be assigned directly to a Java JRadioButton object. The text appears second to the icon.
JRadioButton (Icon icon, boolean selected) This constructor creates a Java JRadioButton object with an Icon. In addition, the status of the button is specified. If the value true is passed as the second parameter, the button has the status “pressed” or the circle of the radio button is filled.
JRadioButton (String text, boolean selected) Here a JRadioButton is created with a text as a label. In addition, the status of the button is specified. If the value true is passed as the second parameter, the button has the status “pressed”.
JRadioButton (String text, Icon icon, boolean selected) Here the previous constructor is extended by the parameter for the icon, which is passed in second place. The parameter that represents the status is now passed in third place.



Example: how to add Java JRadioButtons on window Form using NetBeans:

If you run the above Java source code, you should get the following picture:

Java JRadioButton

As you can see in the screenshot above, the Java JRadioButton component consists of the actual button and the text. Like the checkbox.


Java JRadioButton Example with ActionListener

In this example, you will learn how to use the actionlistener to open a message Dialog box or any other windows in java using Netbeans IDE:

output:

When you run the above code you will see the following output

Java JRadioButton

By default I set the Java JRadiobutton as false, now when you click the anyone of these you will see the message Dialog, i.e. when I click the male radiobutton it will show the following message

Java JRadioButton


Similarly, when I click the female button it will show the following message

Java JRadioButton

The java JRadioButton class is derived from the JToggleButton class and thus also inherits its methods. Java JRadioButton itself has only a few methods of its own, which we consider to be of little relevance at this point and therefore want to leave unmentioned. As you learned from JCheckBox and other controls, you can use the isSelected method to check the status of the Java JRadioButton. In the above examples, we learned how to place and add control to the Java JRadioButton.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button