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
****************************************************************************

Set word wrap in JTextArea

Complete source code below will show you, how to set word wrap in JTextArea.

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


import javax.swing.*;

public class SetWordWrapInJTextArea
{
public static void main(String[]args)
{
JTextArea textArea=new JTextArea();

//Set word wrap in JTextArea
textArea.setWrapStyleWord(true);

//Set line wrap in JTextArea
textArea.setLineWrap(true);

JFrame frame=new JFrame("Set word wrap in JTextArea");

frame.add(textArea);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,500);

frame.setLocationRelativeTo(null);

frame.setVisible(true);
}
}


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

Set JTextArea background color to JFrame default color

Complete source code below will show you, how to set JTextArea background color same to JFrame default color.

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


import javax.swing.*;

import java.awt.Color;

public class SetJTextAreaBackgroundColorToJFrameColor
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JTextArea background color to JFrame color )
JFrame frame=new JFrame("Set JTextArea background color to JFrame color");

//Get JFrame background color
Color jframeColor=frame.getBackground();

//Create a text area with initial text ( Put your text here )
JTextArea textArea=new JTextArea("Put your text here");

//Set text area background color same to JFrame background color
textArea.setBackground(jframeColor);

//Add created text area into JFrame
frame.add(textArea);

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

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

//Make JFrame locate at center on screen
frame.setLocationRelativeTo(null);

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


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

RELAXING NATURE VIDEO