Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

lettion application using radio buttons. 1 Select a color using JRadioButtons An

ID: 3824987 • Letter: L

Question

lettion application using radio buttons. 1 Select a color using JRadioButtons Anderson, Franceschi 5 import javax.swing 6 import java awt. 7 import java awt event. 9 public class changingcolors extends JFrame 10 private container contents; 12 private JRadioButton red, green, blue: 13 private Button Group colorGroup 14 private JLabel label; 15 private color selectedColor Color RED 17 public Changing Colors 19 super "Selecting a color" 20 contents getContent Pane 21 contents set Layout new FlowLayout()): 23 red new JRadioButton red", true); 24 green new JRadioButton "green" 25 blue new JRadioButton "blue" 26 ay background 27 label new JLabel Watch GRAY i 28 label set Foreground color. 29 label true 30 label .setBackground selectedColor 31 32 contents add red); 33 contents add green 34 contents add blue); 35 contents add label) 37 create button group Group 38 color Group new Button 39 colorGroup add( red 40 color Group. add( green 41 add( blue):

Explanation / Answer

Description
   The class extends JFrame. It creates three radio buttons and a label. Also creates a ButtonGroup and adds the three radio buttons to this group. This way, it is ensured that only one radio option can be selected at any time. Listener objects are registered with all the three radio buttons, so apporpriate action can be taken to change the background color of the label based on the option selected. Finally it places all the three radio buttons and a label in a container. The 3 radio buttons are named red, green and blue.

   The listeners objects added to the radio buttons above, is defined by the RadioButtonHandler class. Everytime, some change is made to the radio buttons in the group, "itemStateChanged(..)" method is invoked and all the necessary event information is passed to the method. Based on the type of selected radio button (red, blue or green), the color of the label is changed accordingly.

---------------------------------------------------------------------------------------------------------------------------------


ItemListener and itemStageChange(..)

ItemListener is an interface for receiving the events. The class that wants to process an item's event, must implement this interface. The object created with that class is then registered with a component using addItemListener(...) method. Whenever some change occurs in item selection, the itemStateChange(...) method is invoked immediately. Inside the itemStateChange(...), we define the corresponding actions required!