Home Java Example Java Swing Java JTextArea
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java JTextArea

Advertisement
In this section we will discuss about the javax.swing.JTextArea.

Java JTextArea

In this section we will discuss about the javax.swing.JTextArea.

javax.swing.JTextArea extends the JTextComponent. JTextArea facilitates to display plain text in multiple lines. This is a lightweight component rather than the java.awt.TextArea. This component has more capabilities rather than the java.awt.TextArea. As compare to java.awt.TextArea, javax.swing.JTextArea doesn't manage the scrolling itself. To manage scrolling the swing interface Scrollable is implemented by this class due to this interface this class is capable to be placed within the JScrollpane if JtextArea is required to add scrolling behavior otherwise it can be used without JScrollpane.

Syntax :

public class JTextArea extends JTextComponent

Constructor Detail

Constructor
Description
JTextArea() A default constructor creates a new textarea.
Syntax : public JTextArea()
JTextArea(Document doc) This constructor creates a new JTextArea object with the specified document model.
Syntax : public JTextArea(Document doc)
JTextArea(Document doc, String text, int rows, int columns) This constructor creates a new JTextArea object with the specified document model, string, rows and columns.
Syntax : public JTextArea(Document doc, String text, int rows, int columns)
JTextArea(int rows, int columns) This constructor creates a new JTextArea object with the given number of rows and columns.
Syntax : public JTextArea(int rows, int columns)
JTextArea(String text) This constructor creates a new JTextArea object with the given string.
Syntax : public JTextArea(String text)
JTextArea(String text, int rows, int columns) This constructor creates a new JTextArea object with the given string and the given number of rows and columns.
Syntax : public JTextArea(String text, int rows, int columns)

Method Detail

Some commonly used methods of this class are as follows :

  • append(String str) : This method is used to write the text with appending the existing text. New text is written at the last of the existing text.

    Syntax : public void append(String str)
     
  • getColumns() : This method returns an integer value which specifies the number of columns in the textarea.

    Syntax : public int getColumns()
     
  • getColumnWidth() : This method returns an integer value which specifies the width of column of the textarea.

    Syntax : protected int getColumnWidth()
     
  • getLineCount() : This method returns an integer value which specifies the number of lines in the textarea.

    Syntax : public int getLineCount()
     
  • getLineWrap() : This method is used to specify whether the lines are wrapped or not. The true value specifies that the lines are wrapped wherein, false specifies lines are not wrapped.

    Syntax : public boolean getLineWrap()
     
  • getPreferredScrollableViewportSize() : This method is used to find out the preferred size of the viewport in case the JTextArea is embedded in a JScrollPane.

    Syntax : public Dimension getPreferredScrollableViewportSize()
     
  • getPreferredSize() : This method returns the Dimension of the textarea.

    Syntax : public Dimension getPreferredSize()
     
  • getRowHeight() : This method is used to get the height of row of textarea.

    Syntax : protected int getRowHeight()
     
  • getRows() : This method is use to find out that how many rows are there in the textarea.

    Syntax : public int getRows()
     
  • getTabSize() : This method is used to find out that how many characters are there used for enlarging the tabs. Default value is returned to 8.

    Syntax : public int getTabSize()
     
  • getUIClassID() : This method is used to get the ID of class for the UI.

    Syntax : public String getUIClassID()
     
  • getWrapStyleWord() : This method is used to find out that what wrapping style is (if) used by the textarea.

    Syntax : public boolean getWrapStyleWord()
     
  • insert(String str, int pos) : This method is used to insert the given text at the given position.

    Syntax : public void insert(String str, int pos)
     
  • replaceRange(String str, int start, int end) : This method is used to replace the existing text with the given text started from the given position to the given end position.

    Syntax : public void replaceRange(String str, int start, int end)
     
  • setColumns(int columns) : This method is used to specify the number of columns for the textarea.

    Syntax : public void setColumns(int columns)
     
  • setFont(Font f) : This method is used to specify the current font of text for the textarea.

    Syntax : public void setFont(Font f)
     
  • setLineWrap(boolean wrap) : This method is used to specify the line-wrapping policy of textarea.

    Syntax : public void setLineWrap(boolean wrap)
     
  • setRows(int rows) : This method is used to specify the number of rows for the textarea.

    Syntax : public void setRows(int rows)
     
  • setTabSize(int size) : This method is used to specify the number of characters to enlarger the tabs.

    Syntax : public void setTabSize(int size)
     
  • setWrapStyleWord(boolean word) : This method is used to specify the wrapping style in case of the textarea uses line-wrapping.

    Syntax : public void setWrapStyleWord(boolean word)

Mostly used methods inherited from javax.swing.text.JTextComponent class

Description of the methods given below is in perspective to the JTextArea.

  • getText() : This method is used to find out the text inside the textarea component.

    Syntax : public String getText()
     
  • setText() : This method is used to specify the text into the textarea component.

    Syntax : public void setText(String t)
     
  • isEditable() : This method returns a boolean value which specifies whether the textarea component is editable or not. The value true specifies that the textarea is editable and the false specifies textarea is not editable.

    Syntax : public boolean isEditable()
     
  • setEditable() : This method is used to specify the textarea should be editable or not.

    Syntax : public void setEditable(boolean b)

Except the above methods there are various of methods that are inherited from its parent class.

Example

Here I am going to give a simple example of JTextArea which will demonstrate you about how the JTextArea can be used in the programming. In this example I have created a Java class named JavaJTextAreaExample.java. In this class I have created four different textarea. Different textarea in the sense textarea with different features.

Source Code

JavaJTextAreaExample.java

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.Font;

public class JavaJTextAreaExample
{
     private final static String newline = "\n";     

     JTextArea tarea1= new JTextArea();// creates a default textarea.
     JTextArea tarea2 = new JTextArea(10,20); // creates textarea with three rows and two columns.
     JTextArea tarea3 = new JTextArea("Java JTextArea Example");// creates textarea with the specified text.
     JTextArea tarea4 = new JTextArea("Java JTextArea Example", 3, 2);//creates textarea with the specified    

                                                                                            text and with the three rows and two columns.
     JFrame frame;
     
     public void createUI()
      {         
         frame = new JFrame("JTextArea Example");
         frame.setLayout(null);
         frame.setSize(700,500);
         frame.setVisible(true);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         JLabel lab1 = new JLabel("Default TextArea");
         JLabel lab2 = new JLabel("TextArea with 3 rows and 2 columns");
         JLabel lab3 = new JLabel("TextArea with specified text");
         JLabel lab4 = new JLabel("TextArea with 3 rows and 2 columns with the specified text");

         tarea1.setText("Text set by the user");
         tarea1.append(newline+"NEW TEXT");
         lab1.setBounds(10,40,300,20);
         tarea1.setBounds(370,40,150,80);

         tarea2.setFont(new Font("SERIF", Font.BOLD, 30));         
         lab2.setBounds(10,130,300,20);
         tarea2.setBounds(370,130,150,80);

         tarea3.setEditable(false);
         lab3.setBounds(10,210,300,20);
         tarea3.setBounds(370,220,150,80);

         tarea4.setEditable(true);
         lab4.setBounds(10,300,350,20);
         tarea4.setBounds(370,310,150,80);

         
         frame.add(lab1);        
         frame.add(tarea1);

         frame.add(lab2);        
         frame.add(tarea2);

         frame.add(lab3);        
         frame.add(tarea3);

         frame.add(lab4);        
         frame.add(tarea4);

      }// createUI() closed

      public static void main(String args[])
        {
            JavaJTextAreaExample jtae = new JavaJTextAreaExample();
            jtae.createUI();
        }// main() closed

}// class closed

Output

When you will execute the above example you will get the output as follows :

In the above image first textarea contains text set by setText() method and the text "NEW TEXT " is written into the new line using append() method, in the second textarea I have used the setFont() method and write the text, in the third textarea make it non editable using setEditable(false) method, and makes the fourth textarea editable using setEditable(true) method.

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 3, 2013

Related Tutorials

Ask Questions?    Discuss: Java JTextArea  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Issac Mohanty
August 19, 2013
Not Sufficiant

Not Sufficiant
DMCA.com