Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
Adding Code to the add 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
Before adding code to the
addmethod, 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;Inside the definition of the
addmethod, add the following code:if(m_bReady){ control.setEnabled(checkBox1.getChecked()); groupBox1.add(control); } else super.add(control);This code determines whether the
m_bReadymember variable is set to true. This check is made to prevent the control's GroupBox control from being added to itself. If the value ofm_bReadyis true, the code calls thesetEnabledmethod of the control passed as a parameter to the method. ThesetEnabledmethod 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
addmethod and passes the control parameter to have the control added to the GroupBox control instead of the UserControl. If them_bReadymember variable is set to false, a call is made to the superclass version of theaddmethod with the control passed as a parameter.
Adding Code to Control-Related Methods
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
Inside the definition of the
getControlmethod, type the following code to replace the code that was added by Class Outline:return groupBox1.getControl(index);Inside the definition of the
getControlCountmethod, type the following code to replace the code that was added by Class Outline:return groupBox1.getControlCount();Inside the definition of the
getControls****method, type the following code to replace the code that was added by Class Outline:return groupBox1.getControls();
Adding Code to the remove Method
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
Inside the definition of the
removemethod, 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_bReadyvariable is true. If it is, the code calls the GroupBox control's version of theremovemethod with the control that is passed to the method as a parameter. The check form_bReadybeing true is performed to prevent the CheckBox or GroupBox controls from being removed. Ifm_bReadyis false, the code calls the superclass version of the method to ensure that the control being removed is handled properly.
Adding Code to the setText Method
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
Inside the definition of the
setTextmethod, 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
disposemethod 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 thesetTextmethod.
The next step is to add a new method to the control.