Add scroll bar to JTextArea

Complete source code below will show you, how to add scroll bar to JTextArea.You can use scroll pane constants like below to set your text area scroll bar.

**************
HORIZONTAL
**************
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_NEVER

**************
VERTICAL
**************
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_NEVER

****************************************************************************
COMPLETE SOURCE CODE FOR : AddScrollBarToJTextArea.java
****************************************************************************


import javax.swing.*;

public class AddScrollBarToJTextArea
{
public static void main(String[]args)
{
//Create JTextArea
JTextArea textArea=new JTextArea("Put your text here");

//Create JScrollPane that will be use as JTextArea scrollbar from JTextArea object
JScrollPane scrollBar=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Create a window using JFrame
JFrame frame=new JFrame("Add scrollbar to JTextArea");

//add created JScrollPane into JFrame
frame.add(scrollBar);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,200);

//Make JFrame get to center
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}
}


****************************************************************************
JUST COMPILE AND EXECUTE
****************************************************************************