5

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. :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bradley Odell
  • 1,248
  • 2
  • 15
  • 28

2 Answers2

8

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

Kevin K
  • 9,344
  • 3
  • 37
  • 62
ughzan
  • 1,548
  • 1
  • 14
  • 24
  • Thank you soooo much! I used DocumentListener code and made some custom modifications to the GroupLayout and it works like a charm :D – Bradley Odell Feb 22 '12 at 03:08
  • +1 for `DocumentListener`; see also this related [example](http://stackoverflow.com/q/15039652/230513). – trashgod Feb 23 '13 at 15:50
  • I know this question hasn't been active in 5 years, but in reference to your answer, @ughzan, would the `revalidate()` code also work in a JScrollPane, or any other element that is a parent to the JTextArea? – Ryan Mar 02 '17 at 15:08
  • The reason I ask is because I am working on an application in which the JTextArea needs to do something very similar to this (there isn't really a limit on how large, vertically, the text area needs to be, but I can work around that chunk of code). I have a JTextArea, inside a JPanel, which is inside a JScrollPane. (And, if needed, the JTextArea might go inside its own JScrollPane, with the other list hierarchy unchanged.) I'm trying to get my JTextArea to grow as users enter text into the JTextArea and it becomes "full". Does that make sense? – Ryan Mar 02 '17 at 15:11
  • Here is a link to my question, where I ask this in more detail... Let me know if you need more information in order to answer! I've been working on this issue for a couple of days... http://stackoverflow.com/questions/42548223/how-do-i-dynamically-resize-a-text-component-in-java-netbeans-when-the-text-has/42548267#42548267 – Ryan Mar 02 '17 at 15:16
  • @ughzan I edited my previous comment with your name tagged, because I spelled it wrong. And I'm not sure if editing it will tag you, so I wanted to see if this comment will work. So sorry if I'm being redundant here! – Ryan Mar 02 '17 at 23:20
  • Okay, @ughzan, so I was able to kind of get what I needed out of your code above. However, I'm having trouble with the JTextArea resizing at odd moments. Currently, I am using the KeyPressed event. That will only run the code to update rows and revalidate AFTER a key is pressed. What event should I be looking for to update the row count of the JTextArea? – Ryan Mar 03 '17 at 16:47
2

You can add a DocumentFilter to the jTextArea's Document. IN the filter check row count of your jTextArea and allow/prevent text adding depending on the row count value.

StanislavL
  • 56,971
  • 9
  • 68
  • 98