Adding Code to Overridden Methods

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

  1. Before adding code to the 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;
    
  2. Inside the definition of the 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.

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

  1. Inside the definition of the getControl method, type the following code to replace the code that was added by Class Outline:
    return groupBox1.getControl(index);
    
  2. Inside the definition of the getControlCount method, type the following code to replace the code that was added by Class Outline:
    return groupBox1.getControlCount();
    
  3. 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

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

The next step is to add a new method to the control.