Enabling scroll bars when JTextArea exceeds certain amount of lines

Bradley Odell picture Bradley Odell · Feb 21, 2012 · Viewed 7.2k times · Source

this is my first time using any StackExchange website, so let's see how it goes.

So I've been making a 'local' chat program in Java, and was using a JTextField for chat input. But I wanted to allow for multiline chatting so I switched to JTextArea. I'm using a GroupLayout (built with Window Builder Pro - eclipse) for easy window/component resizing. Here's a picture:

enter image description here

The JTabbedPane, the JTextArea and the Send button are all contained in a JPanel, and all the stuff to the left is in it's own JPanel. So I have the JTextArea and the button docked to the bottom of the right JPanel. The JTextArea is allowed to resize vertically, but the button isn't. I was able to get the JTextArea to grow vertically when I enter new lines, show below:

enter image description here

But I'm unable to think up a way so that if I enter a certain amount of lines into the JTextArea, scrollbars will appear and prevent the JTextArea from taking up any more space. So I tried wrapping the JTextArea in a JScrollPane but disable to scrollbars initially and then enable them when I needed the JTextArea to start scrolling, but I learned that if I wrap it, the JScrollPane won't grow but will still act like it would with the scrollbars visible but... without them. :/

** I wanted to put a link here, but StackOverflow doesn't like me ;)

So, I'm kind of stuck... Is there something that does this that I'm missing? I was thinking that I could just create two different GroupLayout objects, one with the scrollpane not even valid, and then other with the scrollpane valid but stuck at a certain size. On the keyPress listener I could check if the text area exceeds a certain limit, and then it would switch the layout for the panel? The inner JTextArea would still be the same object, but just different layout objects. Opinions on that approach?

Anyway, thanks in advance to all who take their time to answer this. :)

Answer

ughzan picture ughzan · Feb 21, 2012

I wrote a small program that resizes the JTextArea up to a maximum of 4 lines using only Swing controls

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ResizeTextArea {

    public static final int CHAT_ROW_LIMIT = 4;

    public static void main(String[] args) {
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(200, 200));
        topPanel.setBackground(Color.WHITE);

        final JTextArea chatArea = new JTextArea();
        final JScrollPane scrollPane = new JScrollPane(chatArea);

        final JPanel mainPanel = new JPanel(new BorderLayout(5,5));
        mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        mainPanel.add(topPanel, BorderLayout.CENTER);
        mainPanel.add(scrollPane, BorderLayout.SOUTH);

        chatArea.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLineCount();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLineCount();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLineCount();
            }

            private void updateLineCount() {
                int lineCount = chatArea.getLineCount();
                if (lineCount <= CHAT_ROW_LIMIT) {
                    chatArea.setRows(lineCount);
                    mainPanel.revalidate();
                }
            }
        });

        JFrame f = new JFrame("ResizeTextArea");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(mainPanel);
        f.pack();
        f.setVisible(true);
    }
}

Here is how it looks for 1 line, 4 lines, and 8 lines:

1 line4 lines8 lines