Adding and removing controls || Using AWT controls, Layout Managers, and Menus || Bcis Notes

Adding and removing controls || Using AWT controls, Layout Managers, and Menus || Bcis Notes

Adding and removing controls

Programs can be written to add different kinds of controls to your application. You can add single control or you can also add multiple controls to your application by writing an appropriate program. Similarly, you can remove the added control with the help of a program.

In order to include a control in a window, we must add it to the window. So, we must first create an instance of the desired control and then add it to a window by calling add( ), which is defined by Container. The add ( ) method has several forms. The following form is the one that is used for the first part of this
Component add(Component compObj)
Here, compObj is an instance of the control that we want to add. A reference to compObj is returned. Once a control has been added, it will automatically be visible whenever its parent window is displayed. Sometimes we will want to remove a control from a window when the control is no longer needed. For doing this, call remove( ). This method is also defined by Container. It has this general form:

void remove(Component obj)
Here, obj is a reference to the control that we want to remove. We can remove all controls by calling removeAll( ).

To add a control to a collection programmatically

  • Create an instance of the control to be added.
  • Set properties of the new control.
  • Add the control to the Controls collection of the parent control.

To remove controls from a collection programmatically

  • Remove the event handler from the event. In Visual Basic, use the RemoveHandler Statement keyword; in C#, use the -= operator.
  • Use the Remove method to delete the desired control from the panel’s Control collection.
  • Call the Dispose method to release all the resources used by the control.

You may also like AWT Controls Fundaments

Be the first to comment

Leave a Reply

Your email address will not be published.


*