Skip to main content

Java Swing: JTextField, JtextArea.

Java JTextField

The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class.

JTextField class declaration

Let's see the declaration for javax.swing.JTextField class.

  1. public class JTextField extends JTextComponent implements SwingConstants  

Commonly used Constructors:

ConstructorDescription
JTextField()Creates a new TextField
JTextField(String text)Creates a new TextField initialized with the specified text.
JTextField(String text, int columns)Creates a new TextField initialized with the specified text and columns.
JTextField(int columns)Creates a new empty TextField with the specified number of columns.

Commonly used Methods:

MethodsDescription
void addActionListener(ActionListener l)It is used to add the specified action listener to receive action events from this textfield.
Action getAction()It returns the currently set Action for this ActionEvent source, or null if no Action is set.
void setFont(Font f)It is used to set the current font.
void removeActionListener(ActionListener l)It is used to remove the specified action listener so that it no longer receives action events from this textfield.

Java JTextField Example

  1. import javax.swing.*;  
  2. class TextFieldExample  
  3. {  
  4. public static void main(String args[])  
  5.     {  
  6.     JFrame f= new JFrame("TextField Example");  
  7.     JTextField t1,t2;  
  8.     t1=new JTextField("Welcome to Javatpoint.");  
  9.     t1.setBounds(50,100200,30);  
  10.     t2=new JTextField("AWT Tutorial");  
  11.     t2.setBounds(50,150200,30);  
  12.     f.add(t1); f.add(t2);  
  13.     f.setSize(400,400);  
  14.     f.setLayout(null);  
  15.     f.setVisible(true);  
  16.     }  
  17.     }  

Output:

JAVA Jtextfield 1

Java JTextField Example with ActionListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class TextFieldExample implements ActionListener{  
  4.     JTextField tf1,tf2,tf3;  
  5.     JButton b1,b2;  
  6.     TextFieldExample(){  
  7.         JFrame f= new JFrame();  
  8.         tf1=new JTextField();  
  9.         tf1.setBounds(50,50,150,20);  
  10.         tf2=new JTextField();  
  11.         tf2.setBounds(50,100,150,20);  
  12.         tf3=new JTextField();  
  13.         tf3.setBounds(50,150,150,20);  
  14.         tf3.setEditable(false);   
  15.         b1=new JButton("+");  
  16.         b1.setBounds(50,200,50,50);  
  17.         b2=new JButton("-");  
  18.         b2.setBounds(120,200,50,50);  
  19.         b1.addActionListener(this);  
  20.         b2.addActionListener(this);  
  21.         f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);  
  22.         f.setSize(300,300);  
  23.         f.setLayout(null);  
  24.         f.setVisible(true);  
  25.     }         
  26.     public void actionPerformed(ActionEvent e) {  
  27.         String s1=tf1.getText();  
  28.         String s2=tf2.getText();  
  29.         int a=Integer.parseInt(s1);  
  30.         int b=Integer.parseInt(s2);  
  31.         int c=0;  
  32.         if(e.getSource()==b1){  
  33.             c=a+b;  
  34.         }else if(e.getSource()==b2){  
  35.             c=a-b;  
  36.         }  
  37.         String result=String.valueOf(c);  
  38.         tf3.setText(result);  
  39.     }  
  40. public static void main(String[] args) {  
  41.     new TextFieldExample();  
  42. } }  

Output:

JAVA Jtextfield 2

Java JTextArea

The object of a JTextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits JTextComponent class

JTextArea class declaration

Let's see the declaration for javax.swing.JTextArea class.

  1. public class JTextArea extends JTextComponent  

Commonly used Constructors:

ConstructorDescription
JTextArea()Creates a text area that displays no text initially.
JTextArea(String s)Creates a text area that displays specified text initially.
JTextArea(int row, int column)Creates a text area with the specified number of rows and columns that displays no text initially.
JTextArea(String s, int row, int column)Creates a text area with the specified number of rows and columns that displays specified text.

Commonly used Methods:

MethodsDescription
void setRows(int rows)It is used to set specified number of rows.
void setColumns(int cols)It is used to set specified number of columns.
void setFont(Font f)It is used to set the specified font.
void insert(String s, int position)It is used to insert the specified text on the specified position.
void append(String s)It is used to append the given text to the end of the document.

Java JTextArea Example

  1. import javax.swing.*;  
  2. public class TextAreaExample  
  3. {  
  4.      TextAreaExample(){  
  5.         JFrame f= new JFrame();  
  6.         JTextArea area=new JTextArea("Welcome to javatpoint");  
  7.         area.setBounds(10,30200,200);  
  8.         f.add(area);  
  9.         f.setSize(300,300);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.      }  
  13. public static void main(String args[])  
  14.     {  
  15.    new TextAreaExample();  
  16.     }}  

Output:

JAVA Jtextarea 1

Java JTextArea Example with ActionListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class TextAreaExample implements ActionListener{  
  4. JLabel l1,l2;  
  5. JTextArea area;  
  6. JButton b;  
  7. TextAreaExample() {  
  8.     JFrame f= new JFrame();  
  9.     l1=new JLabel();  
  10.     l1.setBounds(50,25,100,30);  
  11.     l2=new JLabel();  
  12.     l2.setBounds(160,25,100,30);  
  13.     area=new JTextArea();  
  14.     area.setBounds(20,75,250,200);  
  15.     b=new JButton("Count Words");  
  16.     b.setBounds(100,300,120,30);  
  17.     b.addActionListener(this);  
  18.     f.add(l1);f.add(l2);f.add(area);f.add(b);  
  19.     f.setSize(450,450);  
  20.     f.setLayout(null);  
  21.     f.setVisible(true);  
  22. }  
  23. public void actionPerformed(ActionEvent e){  
  24.     String text=area.getText();  
  25.     String words[]=text.split("\\s");  
  26.     l1.setText("Words: "+words.length);  
  27.     l2.setText("Characters: "+text.length());  
  28. }  
  29. public static void main(String[] args) {  
  30.     new TextAreaExample();  
  31. }  
  32. }  

Output:

JAVA Jtextarea 2


Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...