Discuss AWT components TextArea, Label briefly with example.

 Label Class

Label is a passive control because it does not create any event when accessed by the user. The label control is an object of Label. A label displays a single line of read-only text. However the text can be changed by the application programmer but cannot be changed by the end user in any way.

Commonly used Constructors

public Label(String strLabel, int alignment);

public Label(String strLabel);

public Label();

Commonly used Public Methods

public String getText(); // returns text of label control

public void setText(String strLabel); //sets label text

public int getAlignment();//returns alignment of label text

public void setAlignment(int alignment); //sets alignment of label text

                  OR,

Java AWT Label

The object of the Label class is a component for placing text in a container. It is used to display a single line of read-only text. The text can be changed by a programmer but a user cannot edit it directly.

It is called a passive control as it does not create an event when it is accessed. To create a label, we need to create the object of the Label class.


AWT Label Class Declaration

public class Label extends Component implements Accessible  

AWT Label Fields

The java.awt component class has the following fields:

  • static int LEFT: It specifies that the label should be left justified.
  • static int RIGHT: It specifies that the label should be right justified.
  • static int CENTER: It specifies that the label should be placed in center.


Java AWT Label Example

In the following example, we are creating two labels l1 and l2 using the Label(String text) constructor and adding them into the frame.

LabelExample.java

import java.awt.*;    

public class LabelExample {    

public static void main(String args[]){   

    // creating the object of Frame class and Label class  

    Frame f = new Frame ("Label example");  

    Label l1, l2;    

    // initializing the labels   

    l1 = new Label ("First Label.");   

    l2 = new Label ("Second Label.");   

    // set the location of label  

    l1.setBounds(50, 100, 100, 30);    

    l2.setBounds(50, 150, 100, 30);  

    // adding labels to the frame    

    f.add(l1);  

    f.add(l2);   

    // setting size, layout and visibility of frame   

    f.setSize(400,400);    

    f.setLayout(null);    

    f.setVisible(true);    

}    

}    


TextField Class

The TextField component allows the user to edit a single line of text. When the user types a key the text field the event is sent to the TextField. The key event may be key pressed, Key release or key typed. The key event is passed to the registered KeyListener. It is also possible to for ActionEvent if the ActionEvent is enabled on the text field.

Commonly used Constructors

public TextField(String strInitialText, int columns); // Construct a TextField instance with the given initial text string with the number of columns.

public TextField(String strInitialText); //Construct a TextField instance with the given initial text string.

public TextField(int columns); // Construct a TextField instance with the number of columns.

Commonly used Public Methods

public String getText(); // Get the current text on this TextField instance

public void setText(String strText);// Set the display text on this TextField instance

public void setEditable(boolean editable);//Set the textfield to editable (read/write) or non-editable (read-only)

                 OR,

Java AWT TextField

The object of a TextField class is a text component that allows a user to enter a single line of text and edit it. It inherits TextComponent class, which further inherits the Component class.

When we enter a key in the text field (like key pressed, key released or key typed), the event is sent to TextField. Then the KeyEvent is passed to the registered KeyListener. It can also be done using ActionEvent; if the ActionEvent is enabled on the text field, then the ActionEvent may be fired by pressing the return key. The event is handled by the ActionListener interface.

AWT TextField Class Declaration

public class TextField extends TextComponent  

Example

// importing AWT class  

import java.awt.*;   

public class TextFieldExample1 {  

    // main method  

    public static void main(String args[]) {    

    // creating a frame  

    Frame f = new Frame("TextField Example");   

    // creating objects of textfield  

    TextField t1, t2;    

    // instantiating the textfield objects  

    // setting the location of those objects in the frame  

    t1 = new TextField("Welcome to Javatpoint.");    

    t1.setBounds(50, 100, 200, 30);    

    t2 = new TextField("AWT Tutorial");    

    t2.setBounds(50, 150, 200, 30);    

    // adding the components to frame  

    f.add(t1);  

    f.add(t2);   

    // setting size, layout and visibility of frame   

    f.setSize(400,400);    

    f.setLayout(null);    

    f.setVisible(true);    

}    

}    



Comments

Popular posts from this blog

Suppose that a data warehouse consists of the three dimensions time, doctor, and patient, and the two measures count and charge, where a charge is the fee that a doctor charges a patient for a visit. a) Draw a schema diagram for the above data warehouse using one of the schemas. [star, snowflake, fact constellation] b) Starting with the base cuboid [day, doctor, patient], what specific OLAP operations should be performed in order to list the total fee collected by each doctor in 2004? c) To obtain the same list, write an SQL query assuming the data are stored in a relational database with the schema fee (day, month, year, doctor, hospital, patient, count, charge)

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?