When you are developing a control, you must often provide methods to perform actions in your control. For the GroupCheck control, you add a method that disables or enables the controls contained within the GroupBox control based on the checked state of the CheckBox control. The method is called from the setChecked
method.
To add a method to a control
public void enableControls(Control start, boolean enable) { for(int i = 0; i < start.getControlCount(); i++) { Control c = start.getControl(i); if(c == groupBox1 || c == checkBox1) { continue; } c.setEnabled(enable); enableControls
(c, enable);
}
}
The enableControls
method accepts a control and a boolean value that determines whether the contained controls should be enabled or disabled. When this method is called by the setChecked
method, it is passed the current instance of the GroupCheck control.
The enableControls
method begins by looping through all the controls that are contained in the start
control parameter. Within the for loop, the code obtains a contained control using the getControl
method with the for loop's current index. If the control is not the GroupCheck control's GroupBox or CheckBox controls, the code enables or disables the specified control based on the value of the enable parameter. Any controls that are contained within this control are then enabled or disabled through a recursive call to enableControls
.
The next step is to add code to the constructor.