This is how the window looks initially.
There are no scrollbars visible. They will be added only if there is sufficient data in the JTextArea to make them necessary.
In this example how to create object of JTextArea and add to the swing application.
The JTextArea component will display the scroll bars once the content big.
We have also given the code of the program discussed here. You can download the code and test on your machine.
Following is the screen shot of the example discussed here.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// components/textarea/TextAreaDemo.java
// Purpose: Illustrate JTextArea, JScrollPane, BorderFactory, ...
// Author: Fred Swartz
// Date : 2000-04-26, ... 2004-11-03
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TextAreaDemo extends JFrame {
//============================================== instance variables
JTextArea m_resultArea = new JTextArea(6, 30);
//====================================================== constructor
public TextAreaDemo() {
//... Set initial text, scrolling, and border.
m_resultArea.setText("Enter more text to see scrollbars");
JScrollPane scrollingArea = new JScrollPane(m_resultArea);
scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
// Get the content pane, set layout, add to center
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.pack();
}
//============================================================= main
public static void main(String[] args) {
JFrame win = new TextAreaDemo();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
|
This is how the window looks after typing in a long line
of text. A horizontal scrollbar is necessary. It's a bit of a puzzle
to me why the vertical scrollbar also appears because it's not needed.
If you stretch the window to make it a little higher, the vertical
scrollbar then disappears.
![]() |