You are here: Home / Topics / Program to use TextField AWT control in Java

Program to use TextField AWT control in Java

Filed under: Java on 2023-10-25 06:39:56

// Program to use TextField AWT control.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="TextFieldDemo" width=350 height=150>
</applet>
*/

public class TextFieldDemo extends Applet implements ActionListener 
{
TextField tfname, tfpass;

public void init() 
{
 Label lblname = new Label("Name: ", Label.RIGHT);
 Label lblpass = new Label("Password: ", Label.RIGHT);
 tfname = new TextField(12);
 tfpass = new TextField(8);
 tfpass.setEchoChar('?');
 
 add(lblname);
 add(tfname);
 
 add(lblpass);
 add(tfpass);

 tfname.addActionListener(this);
 tfpass.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) 
{
 repaint();
}

public void paint(Graphics g) 
{
 g.drawString("Name: " + tfname.getText(), 6, 60);
 g.drawString("Selected text in name: " + tfname.getSelectedText(), 6, 80);
 g.drawString("Password: " + tfpass.getText(), 6, 100);
}
}

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.