Sunday, April 5, 2020

Checkbox class in java

CheckBox In Java
A check box is a graphical component that can be in either an "on" (true) or "off" (false) state. Clicking on a check box changes its state from "on" to "off," or from "off" to "on."
Constructor :-
1.public Checkbox() throws HeadlessException
Creates a check box with no label.
The state of this check box is set to "off," and it is not part of any check box group.
2. public Checkbox(String label)
Creates a check box with the specified label.
The state of this check box is set to "off,"
 it is not part of any check box group.
 Checkbox(String label,boolean state) 
Creates a check box with the specified label and sets the specified state. This check box is not part of any check box group.
4. Checkbox(String label, CheckboxGroup group, boolean state)
Creates a check box with the specified label, in the specified check box group, and set to the specified state. 
  Checkbox constructors:
  • Checkbox( )
  • Checkbox(String str)
  • Checkbox(String str, boolean on)
  • Checkbox(String str, boolean on, CheckboxGroup cbGroup)
  • Checkbox(String str, CheckboxGroup cbGroup, boolean on)
  • These methods 
  • boolean getState( )
  • void setState(boolean on)
  • String getLabel( )
  • void setLabel(String str)
  • Handling Check Boxes
  • Each time a check box is selected or deselected, an item event is generated
  • Each listener implements the ItemListener interface
  • Interface defines the itemStateChanged( ) method.
 Methods in CheckBox


  • getLabel
  • public String getLabel() Gets the label of this check box. Returns the label of this check box, or null if this check box has no label.
  • setLabel
  • public void setLabel(String label)
  • Sets this check box's label to be the string argument.
  •  
  • getState
  • public boolean getState()
  • Determines whether this check box is in the "on" or "off" state.
  • checkbox1.getState());
  • g.drawString("Java: " + java.getState(),10,60);
  • setState
  • public void setState(boolean state Sets the state of this check box to the specified state.
  • Enabling Checkbox checkbox1.setEnabled(false);
  • Checking enable or not
  • (checkbox1.isEnabled()
 Checkbox groups
  • Checkbox groups are collections of checkboxes with the special property that no more than one checkbox in the same group can be selected at a time. The checkboxes in a CheckboxGroup are often called radio buttons. 
  • CheckboxGroup cbg = new CheckboxGroup();
  • To make check boxes act like radio buttons, use this constructor for each Checkbox in the group.
  • public Checkbox(String label, CheckboxGroup cbg, boolean checked)The label is the label for this Checkbox. The CheckboxGroup is the group you want this Checkbox to belong to and must already exist.

No comments:

Post a Comment