Java | AWT Controls

05 min read

AWT Controls

Controls:

Components that allow a user to interact with the application in various ways (Eg, a commonly used control is the push button)

The Layout manager automatically positions components within a container. The LayoutManager is set by the setLayout() method. If no call to setLayout( ) is made, then the default LayoutManager is used

AWT supports the following types of controls:

Labels, push buttons, check boxes, check box groups, lists, scroll bars, text fields etc.

 

Adding Controls:

Create an instance of the desired control and then add it to a window by calling add() method, defined by the Container class.

Syntax:

Component add(Component obj) // obj is the instance of the control to be added

 

Removing Controls:

  • void remove(Component obj) // removes a control from a window when the control is no longer needed
  • void removeAll() // removes all the controls

Almost all the AWT controls may throw a HeadlessException when an attempt is made to instantiate a GUI component, in a non-interactive environment. (such as one in which no display , mouse or keyboard is present)

Label is the only passive control (i.e., they do not generate events). All other controls generate events when they are accessed by the user. Example: When a pushbutton is clicked, an event is sent that identifies the pushbutton.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error