0

I currently have a Java app that is using an instance of JTextArea to present data to the user. They press a button, and data stored from a database is populated.

There is no vertical scrollbar, and the tables in the database contain more rows than can fit on the screen.

How can I add a vertical scrollbar to this text area?

Many portions of the code depend on writing to the JTextArea and it would be a huge time cost to refactor the code to adapt to printing to another type of container.

Is there any way to wrap the JTextArea into a ScrollPane?

Current code (which displays textArea without scroll bars):

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1057, 484);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JTextArea mainTextArea = new JTextArea();

        mainTextArea.setLineWrap(true);
        mainTextArea.setWrapStyleWord(true);
        mainTextArea.setBounds(21, 93, 995, 336);
        frame.getContentPane().add(mainTextArea);
    // (continued...)

My attempt at wrapping the code in a scroll pane (neither the text area nor the scroll bars appear):

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1057, 484);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JTextArea mainTextArea = new JTextArea();

        mainTextArea.setLineWrap(true);
        mainTextArea.setWrapStyleWord(true);
        mainTextArea.setBounds(21, 93, 995, 336);
        JScrollPane scroll = new JScrollPane (MainTextArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(scroll);
        frame.getContentPane().setVisible(true);
    // (continued...)
Queue
  • 446
  • 7
  • 23
  • 2
    *"How can I add a vertical scrollbar to this text area?"* Display it i a `JScrollPane`. But note that won't work when using `null` layouts. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 05 '16 at 02:01
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson May 05 '16 at 02:03
  • 2
    Read the section from the Swing tutorial on [How to Use Text Areas](http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html) for a working example. Download the code and use it as the starting point for your class. The code will be properly structured and follow Swing conventions. Its amazing how Andrew could find so many problems with your 10 lines of code. That is why you need to learn how to code Swing properly by starting with the demo code in the tutorial. And keep a link to the tutorial handy for all the Swing basics. – camickr May 05 '16 at 02:05
  • 1
    `frame.getContentPane().setLayout(null);` is your first problem – MadProgrammer May 05 '16 at 02:31

2 Answers2

1

Couple of mistakes you made

frame.getContentPane().setLayout(null);

  • This statement removes the default BorderLayout associated with JFrame and now it doesn't have any layout.

  • Because the layout of the JFrame became null you need use the setBounds() method for JScrollpane before adding to the JFrame otherwise it will not visible.

  • If you set the something like this scroll.setBounds(21, 93, 995, 336); you will be able to see the JScrollPane added JFrame

NOTE: frame.getContentPane().setVisible(true); will not make your JFrame visible you need change this as frame.setVisible(true).

Always try to use Layouts in swing rather setting null layout.If you use null layout you need specific the setBounds manually which in not good way of designing UI in swing.

Complete working code without using any layout:

package com.jd.swing;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JFrameExample {`enter code here`
private JFrame frame;

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1057, 484);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    JTextArea MainTextArea = new JTextArea();
    MainTextArea.setLineWrap(true);
    MainTextArea.setWrapStyleWord(true);
    MainTextArea.setBounds(21, 93, 995, 336);
    JScrollPane scroll = new JScrollPane(MainTextArea);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setBounds(21, 93, 995, 336);
    frame.getContentPane().add(scroll);
    frame.setVisible(true);
}

public static void main(String[] args) {
    new JFrameExample().initialize();
}
}

Output Screen

0

Just for information,

If you have multiple lines in your text area, the scroll bar is by default scrolled to the end of the text area. To keep the lines in the text area wrapped and scroll bar to the top of the text area, following code would help

MainTextArea.setWrapStyleWord(true);
MainTextArea.setLineWrap(true);
DefaultCaret caret = (DefaultCaret) MainTextArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
PeaceIsPearl
  • 329
  • 2
  • 6
  • 19