JCheckBoxes in Java Swing

Home  »  Java  »  Advance Java »  »  Swing »  JCheckBoxes

The JCheckBox class is the implementation of check boxes in Swing. The functionality is the same as the AWT, with the addition of icon labels.

Constructor methods use the following:

  1. JCheckbox(String) A check box with the specified text label.
  2. JCheckbox(String, boolean) A check box with the specified text label that is selected if the second argument is true.
  3. JCheckbox(Icon) A check box with the specified icon label.
  4. JCheckbox(Icon, boolean) A check box with the specified icon label that is selected if the second argument is true.
  5. JCheckbox(String, Icon) A check box with the specified text label and icon label.
  6. JCheckbox(String, Icon, boolean) A check box with the specified text label and icon label that is selected if the second argument is true.
The following table describes some of the methods provided by the JCheckbox class

MethodsDescription
String getLabel()
void setLabel(String)
Sets or gets a label for a check box
boolean getState()
void setState(boolean)
Sets or gets the status of the check box

Example:

The following example creates two checkboxes one with a text and the other with a text and an icon displayed on it.



Example:

The following example creates four checkboxes, of which one pair will have an icon. The others will have the traditional check box shown. By default the first and the last checkboxes are checked, while the others are not checked. The example uses a method called 'setSelectedIcon()' which is used to replace the previous gif with the gif passed as a parameter to the method.

For example in the following code, checkbox chkIconOne displays an icon 'Back_up.gif'. This icon represents a back arrow. When this checkbox is selected, the method setSelectedIcon() is used which sets 'Back_up.gif' to 'Back_sel.gif'. This represents that the check box is selected.




C:\>jdk1.4\bin>javac JCheckBoxesDemo.java
C:\>jdk1.4\bin>java JCheckBoxesDemo

"Output of JCheckBoxesDemo.class"


Download Complete Program

  -    -  Images




Java Swing-1


JFrame JPanel JApplet
JButtons JLabels JTextFields
JPasswordField JTextAreas JCheckBoxes
JRadioButtons JList JComboBox



No comments:

Post a Comment