Java AWT Components

In this section, you will learn about AWT components and its description.

Java AWT Components



In this section you will learn about the different components available in the Java AWT package for developing user interface for your program.

Following some components of Java AWT are explained : 

  1. Labels : This is the simplest component of Java Abstract Window Toolkit. This component is generally used to show the text or string in your application and label never perform any type of action. Syntax for defining the label only and with justification : 

    Label label_name = new Label ("This is the label text.");
    Above code simply represents the text for the label.
    Label label_name = new Label ("This is the label text.", Label.CENTER);
    Justification of label can be left, right or centered. Above declaration used the center justification of the label using the Label.CENTER.  
  2. Buttons : This is the component of Java Abstract Window Toolkit and is used to trigger actions and other events required for your application. The syntax of defining the button is as follows : 

    Button button_name = new Button ("This is the label of the button.");
    You can change the Button's label or get the label's text by using the Button.setLabel(String) and Button.getLabel() method. Buttons are added to the it's container using the add (button_name) method.
  3. Check Boxes : This component of Java AWT allows you to create check boxes in your applications. The syntax of the definition of Checkbox is as follows : 

    CheckBox checkbox_name = new Checkbox ("Optional check box 1", false);
    Above code constructs the unchecked Checkbox by passing the boolean valued argument false with the Checkbox label through the Checkbox() constructor. Defined Checkbox is added to it's container using add (checkbox_name) method. You can change and get the checkbox's label using the setLabel (String) and getLabel() method. You can also set and get the state of the checkbox using the setState(boolean) and getState() method provided by the Checkbox class.
  4. Radio Button : This is the special case of the Checkbox component of Java AWT package. This is used as a group of checkboxes which group name is same. Only one Checkbox from a Checkbox Group can be selected at a time. Syntax for creating radio buttons is as follows :

    CheckboxGroup chkgp = new CheckboxGroup();
    add (new Checkbox ("One", chkgp, false);
    add (new Checkbox ("Two", chkgp, false);
    add (new Checkbox ("Three",chkgp, false);

    In the above code we are making three check boxes with the label "One", "Two" and  "Three". If you mention more than one true valued for checkboxes then your program takes the last true and show the last check box as checked.
  5. Text Area: This is the text container component of Java AWT package. The Text Area contains plain text. TextArea can be declared as follows:

    TextArea txtArea_name = new TextArea();
    You can make the Text Area editable or not using the setEditable (boolean) method. If you pass the boolean valued argument false then the text area will be non-editable otherwise it will be editable. The text area is by default in editable mode. Text are set in the text area using the setText(string) method of the TextArea class.
  6. Text Field: This is also the text container component of Java AWT package. This component contains single line and limited text information. This is declared as follows :

    TextField txtfield = new TextField(20);
    You can fix the number of columns in the text field by specifying the number in the constructor. In the above code we have fixed the number of columns to 20.


Share on Google+Share on Google+

Java AWT Components

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java AWT Components   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image