Java JTextarea Class Example

JTEXTAREA

Java Swing Tutorial Explaining the JTextArea Component. JTextArea allows editing of multiple lines of text. JTextArea can be used in conjunction with class JScrollPane to achieve scrolling. The underlying JScrollPane can be forced to always or never have either the vertical or horizontal scrollbar.

JTEXTAREA Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTextAreaDemo extends JFrame implements ActionListener {

 JTextField jtfInput;
 JTextArea jtAreaOutput;
 String newline = "\n";
 public JTextAreaDemo() {
  createGui();
 }
 public void createGui() {
  jtfInput = new JTextField(20);
  jtfInput.addActionListener(this);
  jtAreaOutput = new JTextArea(5, 20);
  jtAreaOutput.setCaretPosition(jtAreaOutput.getDocument()
    .getLength());
  jtAreaOutput.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(jtAreaOutput,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  GridBagLayout gridBag = new GridBagLayout();
  Container contentPane = getContentPane();
  contentPane.setLayout(gridBag);
  GridBagConstraints gridCons1 = new GridBagConstraints();
  gridCons1.gridwidth = GridBagConstraints.REMAINDER;
  gridCons1.fill = GridBagConstraints.HORIZONTAL;
  contentPane.add(jtfInput, gridCons1);
  GridBagConstraints gridCons2 = new GridBagConstraints();
  gridCons2.weightx = 1.0;
  gridCons2.weighty = 1.0;
  contentPane.add(scrollPane, gridCons2);
 }
 public void actionPerformed(ActionEvent evt) {
  String text = jtfInput.getText();
  jtAreaOutput.append(text + newline);
  jtfInput.selectAll();
 }
 public static void main(String[] args) {
  JTextAreaDemo jtfTfDemo = new JTextAreaDemo();
  jtfTfDemo.pack();
  jtfTfDemo.addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  jtfTfDemo.setVisible(true);
 }
}

Output
Download JTextArea Source Code

JAVA JTEXTAREA HIERARCHY

javax.swing
Class JTextArea 
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
All Implemented Interfaces: 
Accessible, ImageObserver, MenuContainer, Scrollable, Serializable, SwingConstants
Direct Known Subclasses: 
DefaultTreeCellEditor.DefaultTextField, JFormattedTextField, JPasswordField

JTEXTAREA CONSTRUCTOR

JTextArea()
Constructs a new TextArea.
JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).
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.
JTextArea(int rows, int columns)
Constructs a new empty TextArea with the specified number of rows and columns.
JTextArea(String text)
Constructs a new TextArea with the specified text displayed.
JTextArea(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.

No comments:

Post a Comment