Showing posts with label JTextArea in java. Show all posts
Showing posts with label JTextArea in java. Show all posts

Sunday, 7 April 2019

JTextArea in java | What is JTextArea in java swing with example - Online Help

JTextArea in java:

      The JTextArea in java is a class can be used to enter multiple lines of text. The JTextArea class has four constructors. They are:

JTextArea()

It is used to create an empty JTextArea.

JTextArea(String str)

It is used to create a JTextArea with the specific string.

JTextArea(int numrows,int numcols)

It is used to create an empty JTextArea with the specified number of rows and columns.

JTextArea(String str,int numrows,int numcols)

It is used to create JTextArea with the specified string and the specified number of rows and columns.

Methods for JTextArea in java:

   
    The most commonly used JTextArea() methods are as follows:

void setText(String str)

The setText() method is used to set the string for the fields.

String getText()

The getText() method is used to written the text in the fields.

setLineWrap(Boolean Wrap)

The setLineWrap() method is used to wrap the text to next line,(text reaches the right side of a JTextArea).

void setEditable(boolean state)

The setEditable() method is used to set the fields as being editable or fixed. If the state is true, the JTextArea may be edited. If the state is false, it cannot be edited.

void setFont(Font strfont)

The setFont() method is used to set the font for the JLabel.

void append(String newstr)

The append() method is used to insert the given string to the end of the string which is already in the JTextArea.

Sample program for JTextArea in java :

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JTextAreaDemo extends JApplet implements ActionListener
{
JTextField jtf;
JTextArea jta1;
JTextArea jta2;
Container Panel;
LayoutManager Layout;
public JTextAreaDemo()
{
jtf=new JTextField("jtf",30);
jta1=new JTextArea(5,20);//5 lines of 20 columns
jta2= new JTextArea(5,20);
Layout=new FlowLayout();
Panel=getContentPane();
jtf.addActionListener(this);
jta1.setEditable false();
jta1.setBackground(Color.yellow);
jta1.setLineWrap(true);
Panel.setLayout(Layout);
Panel.add(jtf);
Panel.add(jta1);
Panel.add(jta2);
Panel.setBackground(Color.red);
}
public void actionPerformed(ActionEvent ae)
{
String output;
output="enter text of the JTextField are\""+jtf.getText()+"\",and this is jta1 with text wrap";
jta1.setText(output);
output="enter the text of the JTextField are\""+jtf.getText()+"\",and this is jta2"
jta2.setText(output);
}
}