Java AWT Components

Java AWT Components

After having the basic idea of GUI, let us know how many Java AWT Components classes exist with java.awt package. This knowledge is essential to go further in the tutorial.

Java AWT Components

As you can observe from the above hierarchy, Component is the super class of all Java components and is declared as abstract. That is, we cannot create objects of Component class directly.

Following is the class signature

public abstract class Component extends Object implements ImageObserver, MenuContainer, Serializable

Properties of Java AWT Components
  1. A Component object represents a graphical interactive area displayable on the screen that can be used by the user.
  2. Any subclass of a Component class is known as a component. For example, button is a component.
  3. Only components can be added to a container, like frame.
Important methods of Component class

Following are the important methods, practiced very often, of Component class that can be used by all the sub classes (components).

  1. setEnabled(boolean): The parameter false makes the component disabled so that it does not respond to user interactions (say, clicks).
  2. setBounds(): Using this method, the programmer can give his own size to the component in terms of width and height and also the location where he can place the component in the container. This method is not used often as the programmer prefers to place the component in the container using layout managers.
  3. getWidth(): The programmer can obtain the width of the component.
  4. getHeight(): The programmer can obtain the height of the component.
  5. paint(): Used very extensively to draw graphics. This method is called implicitly when the frame is created or resized.
  6. repaint(): Used by the programmer to call the paint() method explicitly anywhere in the program.
  7. setBackground(): Used to give a background color to a component.
  8. setForeground(): Used to give a foreground color to a component.
  9. setFont(): Used to give a font to a component to display the text.
  10. getSize(): Returns the size of the component.
  11. setSize(): Used to give the size to the component.
  12. update(): The is method is called implicitly by the repaint(). A call to this method clears the earlier drawings existing on the container.

Leave a Reply

Your email address will not be published. Required fields are marked *