Bitcoin

Bitcoin
Bitcoin

JTextArea Java

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  


Class Constructors


Constructor & Description
1
JTextArea()
Constructs a new TextArea.
2
JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).
3
JTextArea(Document doc, String text, int rows, int columns)
Constructs a new JTextArea with the specified number of rows and columns, and the given model.
4
JTextArea(int rows, int columns)
Constructs a new empty TextArea with the specified number of rows and columns.
5
JTextArea(String text)
Constructs a new TextArea with the specified text displayed.
6
JTextArea(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.

Class Methods


Method & Description
1
void append(String str)
Appends the given text to the end of the document.
2
protected Document createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given.
3
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JTextArea.
4
int getColumns()
Returns the number of columns in the TextArea.
5
protected int getColumnWidth()
Gets column width.
6
int getLineCount()
Determines the number of lines contained in the area.
7
int getLineEndOffset(int line)
Determines the offset of the end of the given line.
8
int getLineOfOffset(int offset)
Translates an offset into the components text to a line number.
9
int getLineStartOffset(int line)
Determines the offset of the start of the given line.
10
boolean getLineWrap()
Gets the line-wrapping policy of the text area.
11
Dimension getPreferredScrollableViewportSize()
Returns the preferred size of the viewport if this component is embedded in a JScrollPane.
12
Dimension getPreferredSize()
Returns the preferred size of the TextArea.
13
protected int getRowHeight()
Defines the meaning of the height of a row.
14
int getRows()
Returns the number of rows in the TextArea.
15
boolean getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.
16
int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation.
17
int getTabSize()
Gets the number of characters used to expand tabs.
18
String getUIClassID()
Returns the class ID for the UI.
19
boolean getWrapStyleWord()
Gets the style of wrapping used if the text area is wrapping lines.
20
void insert(String str, int pos)
Inserts the specified text at the specified position.
21
protected String paramString()
Returns a string representation of this JTextArea.
22
void replaceRange(String str, int start, int end)
Replaces text from the indicated start to end position with the new text specified.
23
void setColumns(int columns)
Sets the number of columns for this TextArea.
24
void setFont(Font f)
Sets the current font.
25
void setLineWrap(boolean wrap)
Sets the line-wrapping policy of the text area.
26
void setRows(int rows)
Sets the number of rows for this TextArea.
27
void setTabSize(int size)
Sets the number of characters to expand tabs to.
28
void setWrapStyleWord(boolean word)
Sets the style of wrapping used, if the text area is wrapping lines.

Methods Inherited

This class inherits methods from the following classes −
  • javax.swing.text.JTextComponent
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JTextArea Example

Create the following Java program using any editor of your choice in say D:/ > SWING > com > sjavaspot > gui >

In the program above:
  • First, we create a new JTextArea instance ta and init its rows and columns
  • Then, we ccreate a new instance of JScrollPane and pass the JTextArea instance to its constructor.  This adds horizontal and vertical scrollbars.
In this tutorial, you’ve learned how to use JTextArea class to create text area widget. We have also shown you how to use JTextArea class with JScrollPane class to add horizontal and vertical scrollbar.
Here is the screenshot of the demo application:

JTextArea
Other JTextArea Examples
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JTextAreaTest {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JTextArea Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "A JTextArea object represents a multiline area for displaying text. "
        + "You can change the number of lines that can be displayed at a time, "
        + "as well as the number of columns. You can wrap lines and words too. "
        + "You can also put your JTextArea in a JScrollPane to make it scrollable.";
    JTextArea textAreal = new JTextArea(text, 5, 10);
    textAreal.setPreferredSize(new Dimension(100, 100));
    JTextArea textArea2 = new JTextArea(text, 5, 10);
    textArea2.setPreferredSize(new Dimension(100, 100));
    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textAreal.setLineWrap(true);
    textArea2.setLineWrap(true);
    frame.add(textAreal);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
  }
}
Output

Using JTextArea

Another Example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingJTextAreaDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingJTextAreaDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingJTextAreaDemo  swingJTextAreaDemo = new SwingJTextAreaDemo();      
      swingJTextAreaDemo.showTextAreaDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showTextAreaDemo(){
      headerLabel.setText("Control in action: JTextArea"); 
      JLabel  commentlabel= new JLabel("Comments: ", JLabel.RIGHT);
      
      final JTextArea commentTextArea = 
         new JTextArea("This is a Swing tutorial " 
         +"to make GUI application in Java.",5,20);

      JScrollPane scrollPane = new JScrollPane(commentTextArea);    
      JButton showButton = new JButton("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            statusLabel.setText( commentTextArea.getText());        
         }
      }); 
      controlPanel.add(commentlabel);
      controlPanel.add(scrollPane);        
      controlPanel.add(showButton);
      mainFrame.setVisible(true);  
   }
}
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\sjavaspot\gui\SwingJTextAreaDemo.java
If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.sjavaspot.gui.SwingJTextAreaDemo
Verify the following output.
Swing JTextArea

No comments:

Post a Comment

Facebook