Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:39:39+00:00 2026-05-30T11:39:39+00:00

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

  • 0

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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:39:40+00:00Added an answer on May 30, 2026 at 11:39 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time using this site so sorry for any bad formatting
This is my first time using JQuery in any of my projects. I have
This is my first time using any sort of version control (git) and excited
This is my first time using PayPal to handle payments from a website, and
This is my first time using joomla. I don't know if I'm using the
this is my first time using StAX for parsing XML documents (still in the
I'm fairly new to both Django and Python. This is my first time using
This is the first time ever I'm using AJAX, and I want to do
I'm using this XML classes for the first time and can't find this piece
I'm using this line to get the beginning time of the first day of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.