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.
- public class JTextField extends JTextComponent implements SwingConstants
Commonly used Constructors:
Constructor | Description |
---|
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:
Methods | Description |
---|
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
- import javax.swing.*;
- class TextFieldExample
- {
- public static void main(String args[])
- {
- JFrame f= new JFrame("TextField Example");
- JTextField t1,t2;
- t1=new JTextField("Welcome to Javatpoint.");
- t1.setBounds(50,100, 200,30);
- t2=new JTextField("AWT Tutorial");
- t2.setBounds(50,150, 200,30);
- f.add(t1); f.add(t2);
- f.setSize(400,400);
- f.setLayout(null);
- f.setVisible(true);
- }
- }
Output:
Java JTextField Example with ActionListener
- import javax.swing.*;
- import java.awt.event.*;
- public class TextFieldExample implements ActionListener{
- JTextField tf1,tf2,tf3;
- JButton b1,b2;
- TextFieldExample(){
- JFrame f= new JFrame();
- tf1=new JTextField();
- tf1.setBounds(50,50,150,20);
- tf2=new JTextField();
- tf2.setBounds(50,100,150,20);
- tf3=new JTextField();
- tf3.setBounds(50,150,150,20);
- tf3.setEditable(false);
- b1=new JButton("+");
- b1.setBounds(50,200,50,50);
- b2=new JButton("-");
- b2.setBounds(120,200,50,50);
- b1.addActionListener(this);
- b2.addActionListener(this);
- f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
- f.setSize(300,300);
- f.setLayout(null);
- f.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- String s1=tf1.getText();
- String s2=tf2.getText();
- int a=Integer.parseInt(s1);
- int b=Integer.parseInt(s2);
- int c=0;
- if(e.getSource()==b1){
- c=a+b;
- }else if(e.getSource()==b2){
- c=a-b;
- }
- String result=String.valueOf(c);
- tf3.setText(result);
- }
- public static void main(String[] args) {
- new TextFieldExample();
- } }
Output:
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.
- public class JTextArea extends JTextComponent
Commonly used Constructors:
Constructor | Description |
---|
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:
Methods | Description |
---|
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
- import javax.swing.*;
- public class TextAreaExample
- {
- TextAreaExample(){
- JFrame f= new JFrame();
- JTextArea area=new JTextArea("Welcome to javatpoint");
- area.setBounds(10,30, 200,200);
- f.add(area);
- f.setSize(300,300);
- f.setLayout(null);
- f.setVisible(true);
- }
- public static void main(String args[])
- {
- new TextAreaExample();
- }}
Output:
Java JTextArea Example with ActionListener
- import javax.swing.*;
- import java.awt.event.*;
- public class TextAreaExample implements ActionListener{
- JLabel l1,l2;
- JTextArea area;
- JButton b;
- TextAreaExample() {
- JFrame f= new JFrame();
- l1=new JLabel();
- l1.setBounds(50,25,100,30);
- l2=new JLabel();
- l2.setBounds(160,25,100,30);
- area=new JTextArea();
- area.setBounds(20,75,250,200);
- b=new JButton("Count Words");
- b.setBounds(100,300,120,30);
- b.addActionListener(this);
- f.add(l1);f.add(l2);f.add(area);f.add(b);
- f.setSize(450,450);
- f.setLayout(null);
- f.setVisible(true);
- }
- public void actionPerformed(ActionEvent e){
- String text=area.getText();
- String words[]=text.split("\\s");
- l1.setText("Words: "+words.length);
- l2.setText("Characters: "+text.length());
- }
- public static void main(String[] args) {
- new TextAreaExample();
- }
- }
Output:
Anurag Rana
Educator CSE/IT
Comments
Post a Comment