Search This Blog

ADVANCED JAVA

C-PROGRAMMING

Wednesday, December 28, 2022

Java AWT TextField

 

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. no.ConstructorDescription
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 columns)It constructs a new text field with the given text and given number of columns (width).

TextField Class Methods

Sr. no.Method nameDescription
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 processActionEvent(ActionEvent e)It processes action events occurring in the text field 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 l)It removes specified action listener so that it 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 Javatpoint.");    
  14.     t1.setBounds(5010020030);    
  15.     t2 = new TextField("AWT Tutorial");    
  16.     t2.setBounds(5015020030);    
  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

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(505015020);    
  15.         tf2 = new TextField();    
  16.         tf2.setBounds(5010015020);    
  17.         tf3 = new TextField();    
  18.         tf3.setBounds(5015015020);  
  19.         tf3.setEditable(false);   
  20.         b1 = new Button("+");    
  21.         b1.setBounds(502005050);  
  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:

java awt textfield

0 comments:

Post a Comment

OS

Top