After you have created overridden methods using Class Outline, you provide your implementation code to the method definitions. Depending on how you implement the overridden method, you can either retain or remove the call to the superclass version of the method.
To add controls to the GroupCheck control's GroupBox control, you add code to the GroupCheck's add
method that calls the GroupBox's add
method. This causes the GroupBox control, instead of the GroupCheck control, to parent the control being added.
To add code to the add method
add
method, you add a private member variable to the GroupCheck class to determine whether controls can be added. Add the following line of code to the GroupCheck class:private boolean m_bReady = false;
add
method, add the following code:if(m_bReady){
control.setEnabled(checkBox1.getChecked());
groupBox1.add(control);
}
else
super.add(control);
This code determines whether the m_bReady
member variable is set to true. This check is made to prevent the control's GroupBox control from being added to itself. If the value of m_bReady
is true, the code calls the setEnabled
method of the control passed as a parameter to the method. The setEnabled
method is passed the checked state of the GroupCheck control's CheckBox control. Because controls can be added to the GroupCheck control when it is unchecked, it is important that controls be enabled or disabled properly when added.
The code then calls the GroupBox control's add
method and passes the control parameter to have the control added to the GroupBox control instead of the UserControl. If the m_bReady
member variable is set to false, a call is made to the superclass version of the add
method with the control passed as a parameter.
So that the user can access the controls within the GroupBox control, you provide code in the getControl
, getControlCount
, and getControls
methods that calls the GroupBox control's implementation of these methods.
To add code to control-related methods
getControl
method, type the following code to replace the code that was added by Class Outline:return groupBox1.getControl(index);
getControlCount
method, type the following code to replace the code that was added by Class Outline:return groupBox1.getControlCount();
getControls
method, type the following code to replace the code that was added by Class Outline:return groupBox1.getControls();
So that controls can be deleted from the GroupCheck control, you provide code in the remove
method for the GroupCheck control that calls the GroupBox control's remove
method.
To add code to the remove method
remove
method, type the following code to replace the code that was added by Class Outline:if(m_bReady) {
groupBox1.remove(c);
}
else {
super.remove(c);
}
This code determines whether the m_bReady
variable is true. If it is, the code calls the GroupBox control's version of the remove
method with the control that is passed to the method as a parameter. The check for m_bReady
being true is performed to prevent the CheckBox or GroupBox controls from being removed. If m_bReady
is false, the code calls the superclass version of the method to ensure that the control being removed is handled properly.
The GroupBox control does not provide a way to autosize the text portion of the control to the amount of text being displayed. For the GroupCheck control to display its text properly, override the setText
method to determine the correct width of the control based on the size of the text to display.
To add code to the setText method
setText
method, type the following code to replace the code that was added by Class Outline:Graphics g = checkBox1.createGraphics();
checkBox1.setWidth(g.getTextSize(value).x + 20);
g.dispose();
checkBox1.setText(value);
super.setText(value);
This code uses the Graphics class methods to determine the size of the text that is being specified. When the size of the text is determined, it is increased by a value of 20 to compensate for the size of the check box and the space between the check box and the text portion of the CheckBox control. The code then calls the dispose
method of the Graphics class to free any resources that were allocated, sets the text property of the CheckBox control, and calls the superclass version of the setText
method.
The next step is to add a new method to the control.