0% found this document useful (0 votes)
27 views

Java AWT Label & Textfield

The document discusses the Java AWT Label class which is used to display read-only text in a container. It describes the label class declaration, fields, constructors and methods. Examples provided demonstrate creating labels with different text and alignments and adding them to a frame.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Java AWT Label & Textfield

The document discusses the Java AWT Label class which is used to display read-only text in a container. It describes the label class declaration, fields, constructors and methods. Examples provided demonstrate creating labels with different text and alignments and adding them to a frame.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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 any event when it is accessed. To create a label, we need to create the object
of Label class.

AWT Label Class Declaration


1. public class Label extends Component implements Accessible

AWT Label Fields


The java.awt.Component class has following fields:

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

Label class Constructors

Sr. Constructor Description


no.
1. Label() It constructs an empty label.

2. Label(String text) It constructs a label with the given string (left justified by
default).

3. Label(String text, int It constructs a label with the specified string and the specified
alignement) alignment.

Label Class Methods


Specified

Formatted: Justified, Space Before: Auto, After: Auto, Font Alig

Play Videox

Sr. Method name Description


no.
1. void setText(String text) It sets the texts for label with the specified text.

2. void setAlignment(int alignment) It sets the alignment for label with the specified
alignment.

3. String getText() It gets the text of the label

4. int getAlignment() It gets the current alignment of the label.

5. void addNotify() It creates the peer for the label.

6. AccessibleContext It gets the Accessible Context associated with the


getAccessibleContext() label.

7. protected String paramString() It returns the string the state of the label.

Method inherited
The above methods are inherited by the following classes:

o java.awt.Component
o java.lang.Object

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
1. import java.awt.*;
2.
3. public class LabelExample {
4. public static void main(String args[]){
5.
6. // creating the object of Frame class and Label class
7. Frame f = new Frame ("Label example");
8. Label l1,
l2; 9.
10. // initializing the labels
11. l1 = new Label ("First Label.");
12. l2 = new Label ("Second
Label."); 13.
14. // set the location of label
15. l1.setBounds(50, 100, 100, 30);
16. l2.setBounds(50, 150, 100, 30);
17.
18. // adding labels to the frame
19. f.add(l1);
20. f.add(l2);
21.
22. // setting size, layout and visibility of
frame 23. f.setSize(400,400);
24. f.setLayout(null);
25. f.setVisible(true);
26. }
27. }

Output:

Java AWT Label Example with ActionListener


In the following example, we are creating the objects of TextField, Label and Button classes and adding them to the Frame. Using the
actionPerformed() method an event is generated over the button. When we add the website in the text field and click on the
button, we get the IP address of website.

LabelExample2.java

1. import java.awt.*;
2. import java.awt.event.*;
3.
4. // creating class which implements ActionListener interface and inherits Frame class
5. public class LabelExample2 extends Frame implements ActionListener{
6.
7. // creating objects of TextField, Label and Button class
8. TextField tf;
9. Label l;
10. Button b;
11.
12. // constructor to instantiate the above objects
13. LabelExample2() {
14. tf = new TextField();
15. tf.setBounds(50, 50, 150, 20);
16.
17. l = new Label();
18. l.setBounds(50, 100, 250, 20);
19.
20. b = new Button("Find
IP"); 21. b.setBounds(50,150,60,30);
22. b.addActionListener(this);
23.
24. add(b);
25. add(tf);
26. add(l);
27.
28. setSize(400,400);
29. setLayout(null);
30. setVisible(true);
31. }
32.
33. // defining actionPerformed method to generate an event
34. public void actionPerformed(ActionEvent e) {
35. try {
36. String host = tf.getText();
37. String ip = java.net.InetAddress.getByName(host).getHostAddress();
38. l.setText("IP of "+host+" is: "+ip);
39. }
40. catch (Exception ex) {
41. System.out.println(ex);
42. }
43. }
44.
45. // main method
46. public static void main(String[] args) {
47. new LabelExample2();
48. }
49. }
Java AWT TextField
The object of a TextField class is a text component that allows a user to enter a single line text and edit it. It
inherits TextComponent class, which further inherits 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 return key. The event is handled by the ActionListener interface.

AWT TextField Class Declaration


1. public class TextField extends TextComponent

TextField Class constructors

Sr. Constructor Description


no.

1. TextField() It constructs a new text field component.

2. TextField(String text) It constructs a new text field initialized with the given string text
to be displayed.

3. TextField(int columns) It constructs a new textfield (empty) with given number of


columns.
4. TextField(String text, int It constructs a new text field with the given text and given number
columns) of columns (width).

TextField Class Methods

Sr. Method name Description


no.

1. void addNotify() It creates the peer of text field.

2. boolean echoCharIsSet() It tells whether text field has character set for echoing
or not.

3. void addActionListener(ActionListener l) It adds the specified action listener to receive


action events from the text field.

4. ActionListener[] getActionListeners() It returns array of all action listeners registered on text


field.

5. AccessibleContext getAccessibleContext() It fetches the accessible context related to the text


field.

6. int getColumns() It fetches the number of columns in text field.

7. char getEchoChar() It fetches the character that is used for echoing.

8. Dimension getMinimumSize() It fetches the minimum dimensions for the text field.
9. Dimension getMinimumSize(int columns) It fetches the minimum dimensions for the text
field with specified number of columns.

10. Dimension getPreferredSize() It fetches the preferred size of the text field.

11. Dimension getPreferredSize(int columns) It fetches the preferred size of the text field with
specified number of columns.

12. protected String paramString() It returns a string representing state of the text field.

13. protected void It processes action events occurring in the text field
processActionEvent(ActionEvent e) by dispatching them to a registered ActionListener
object.

14. protected void processEvent(AWTEvent e) It processes the event on text field.

15. void removeActionListener(ActionListener It removes specified action listener so that it


l) doesn't receive action events anymore.

16. void setColumns(int columns) It sets the number of columns in text field.

17. void setEchoChar(char c) It sets the echo character for text field.

18. void setText(String t) It sets the text presented by this text component to
the specified text.

Method Inherited
The AWT TextField class inherits the methods from below classes:
1. java.awt.TextComponent
2. java.awt.Component
3. java.lang.Object

Java AWT TextField Example


TextFieldExample1.java

1. // importing AWT class


2. import java.awt.*;
3. public class TextFieldExample1 {
4. // main method
5. public static void main(String args[]) {
6. // creating a frame
7. Frame f = new Frame("TextField Example");
8.
9. // creating objects of textfield
10. TextField t1, t2;
11. // instantiating the textfield objects
12. // setting the location of those objects in the frame
13. t1 = new TextField("Welcome to aics.");
14. t1.setBounds(50, 100, 200, 30);
15. t2 = new TextField("AWT TextField");
16. t2.setBounds(50, 150, 200, 30);
17. // adding the components to frame
18. f.add(t1);
19. f.add(t2);
20. // setting size, layout and visibility of
frame 21. f.setSize(400,400);
22. f.setLayout(null);
23. f.setVisible(true);
24. }
25. }

Output:

Java AWT TextField Example with ActionListener


TextFieldExample2.java

1. // importing necessary libraries


2. import java.awt.*;
3. import java.awt.event.*;
4. // Our class extends Frame class and implements ActionListener interface
5. public class TextFieldExample2 extends Frame implements ActionListener {
6. // creating instances of TextField and Button class
7. TextField tf1, tf2, tf3;
8. Button b1, b2;
9. // instantiating using constructor
10. TextFieldExample2() {
11. // instantiating objects of text field and button
12. // setting position of components in frame
13. tf1 = new TextField();
14. tf1.setBounds(50, 50, 150, 20);
15. tf2 = new TextField();
16. tf2.setBounds(50, 100, 150, 20);
17. tf3 = new TextField();
18. tf3.setBounds(50, 150, 150, 20);
19. tf3.setEditable(false);
20. b1 = new Button("+");
21. b1.setBounds(50, 200, 50, 50);
22. b2 = new Button("-");
23. b2.setBounds(120,200,50,50);
24. // adding action listener
25. b1.addActionListener(this);
26. b2.addActionListener(this);
27. // adding components to frame
28. add(tf1);
29. add(tf2);
30. add(tf3);
31. add(b1);
32. add(b2);
33. // setting size, layout and visibility of
frame 34. setSize(300,300);
35. setLayout(null);
36. setVisible(true);
37. }
38. // defining the actionPerformed method to generate an event on buttons
39. public void actionPerformed(ActionEvent e) {
40. String s1 = tf1.getText();
41. String s2 = tf2.getText();
42. int a = Integer.parseInt(s1);
43. int b = Integer.parseInt(s2);
44. int c = 0;
45. if (e.getSource() == b1){
46. c = a + b;
47. }
48. else if (e.getSource() == b2){
49. c = a - b;
50. }
51. String result = String.valueOf(c);
52. tf3.setText(result);
53. }
54. // main method
55. public static void main(String[] args) {
56. new TextFieldExample2();
57. }
58. }

Output:

You might also like