Java AWT Label & Textfield
Java AWT Label & Textfield
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.
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.
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.
Play Videox
2. void setAlignment(int alignment) It sets the alignment for label with the specified
alignment.
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
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:
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.
2. TextField(String text) It constructs a new text field initialized with the given string text
to be displayed.
2. boolean echoCharIsSet() It tells whether text field has character set for echoing
or not.
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.
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
Output:
Output: