Check Boxes

The Check Box class, which provides the functionality of a check box, is a concrete
implementation of Abstract Button. Some of its constructors are shown here:
JCheckBox(lcon I)
JCheckBox(Icon i, boolean slate)
JCheckBox(String s)
JCheckBox(String 5, boolean state)
JCheckBox(String 5, Icon I)
JCheckBox(String 5, Icon i, boolean state)
Here, j is the icon for the button. The text is specified by s. If state is true, the check box
is initially selected. Otherwise, it is not.
The state of the check box can be changed via the following method:

Here, state is true if the check box should be checked. The following example illustrates how to create an applet that displays four check boxes and a text field. When n check box is pressed, its text is displayed in the text field. The content pane for the JApplet object is obtained, and a flow layout is assigned as its layout manager. Next, four check boxes are added to the content pane, and icons are assigned for the normal, rollover, and selected states. The applet is then registered to receive item events. Finally, a text field Isadded to the content pane.
When a check box is selected or deselected, an item event is generated. This is
handled by itemStateChartgcd( ).lnside itcmStateChnnscd( ), the getlteil( ) method
gets the JCheckDox object thnt generated the event. The getText( ) method gets the text (or that check box and uses it to set the text inside the text field.

Radio Buttons

]Radio buttons arc supported by the ]RadioButton class, which is n concrete
Implementntlon of Abstractllutton. Some of its constructors are shown here:
JR.\dioButton(Icon i)
JRadioButton(Jcon i, boolean staft»
JRadioButton(String s)
JRadioButton(String 5, boolean state)
JRndioButton(String s, Icon 😉
JRadloButton(String s, Icon i, boole,nn stale)
Here, ; Is the Icon (or the button. The text is specified by s, If stile is true, the button is  Initially selected. Otherwise, it is not Radio buttons must be configured into n group. Only one of the.buttons in that group can be selected nature time. Poe example, If n user presses n radio button tent is
in “‘group, any previously selected button in that group Is automatically deselected.
The class  to create n button group. Its default constructor Is Invoked Cor this purpose. Elements are then added to the button stoup via t he following method:
void Hero, nil  reference to the button to be added to the Group. Tho following example illustrious how to use radio buncos. Three radio buttons
and one text field arc created. When it radio button I pressed, Its text is displayed In
the text field. First, the content pane for the Applicant object is obtained and’a flow
layout Is assigned as its layout crumb. Next, three radio buttons arc added to the .
content pane. Then, a button- group is defined and the buttons arc added to it. Finally  text field is added to the content pane.
Radio button presses rotten action events that arc handled by undermentioned )
The commendation( ) method gels the text that is associated with a radio button and uses it to set the text field.

Combo Boxes

Swing provides a Colombo box (a combination of a text field and a drop-down list)
through the J Combo Box class, which extends Compote  combo. box normally
displays one entry. However, it can also display a drop-down list that allows a user to
select a different entry. You can also type your selection into the text field.Two of
J Combo Dox’s constructors are shown here:
J Combo Box( )
J Combo Box(Vector v)
Here, v is a vector that initializes the combo box.
Items arc added to Panelist of choices via the addle ) method, whose signature is
shown here:
void Odd Item(Object))
Here, obj is the object to be added to the combo box.
the following example contains a combo box and a label. The label display an icon. The combo box contains entries  “France” “Germany”Italy” Whim it country is selected, the label is updated to display the nag for that courtier.

reCAPTCHA is required.

Share This