Showing posts with label scroll bar. Show all posts
Showing posts with label scroll bar. Show all posts

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

Add scrollbar to JFrame

Source code below will show you, how to add scrollbar to a JFrame.

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


import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AddScrollBarToJFrame
{
public static void main(String[]args)
{
//Create a JPanel
JPanel panel=new JPanel();

//Create a scrollbar using JScrollPane and add panel into it's viewport
//Set vertical and horizontal scrollbar always show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Create a JFrame with title ( AddScrollBarToJFrame )
JFrame frame=new JFrame("AddScrollBarToJFrame");

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

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

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);

//So, if you want to add other component like JTextArea, just add them into JPanel.After that add
//the JPanel into JScrollPane before add the JScrollPane into JFrame.
}
}


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

RELAXING NATURE VIDEO