Output:
Code:
/** * File: programJTextArea.java * Tiltle: How to Program a JTextArea * Author: http://java-program-sample.blogspot.com */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class programJTextArea extends JFrame { //Initializing JTextArea private JTextArea txtArea; //Setting up GUI public programJTextArea() { //Setting up the Title of the Window super("How to Program a JTextArea"); //Set Size of the Window (WIDTH, LENGTH) setSize(290,357); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing JTextArea txtArea = new JTextArea(); //Constructing a JScrollPane for JTextArea's Horizontal and Vertical Scroll Bar JScrollPane scroller = new JScrollPane(txtArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Setting up the container layout GridLayout grid = new GridLayout(1,1); pane.setLayout(grid); //Adding the JScrollPane to the Container with JTextArea on it pane.add(scroller); /**Set all the Components Visible. * If it is set to "false", the components in the container will not be visible. */ setVisible(true); } //Main Method public static void main (String[] args) { programJTextArea pjtf = new programJTextArea(); } }
Important Part of the Program:
//Constructing JTextArea JTextArea txtArea = new JTextArea(); //Constructing a JScrollPane for JTextArea's Horizontal and Vertical Scroll Bar JScrollPane scroller = new JScrollPane(txtArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);