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

  • Home
  • SEARCH
  • 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 4595250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:56:37+00:00 2026-05-21T22:56:37+00:00

I start off by creating a JTextArea of a specific size. The user can

  • 0

I start off by creating a JTextArea of a specific size. The user can add text within it but it will get cut off if it becomes too long (vertically or horizontally). I want the JTextArea to automatically expand or contract (for deletion of text).

I may allow users to change font and font size in the future, so it would be good if I could avoid making things increase/decrease by a certain size.

I am currently using bounds to size my JTextArea. Perhaps I should size by rows and columns and use a listener and act appropriately?

thanks in advance!

  • 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-21T22:56:38+00:00Added an answer on May 21, 2026 at 10:56 pm

    I can’t imagine why you’d want to do this, why not put a JTextArea in a JScrollPane, but ok, i’ll play along… Maybe something like this:

    import java.awt.Container;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.BadLocationException;
    
    
    public class Expander extends JFrame {
    
        private final JTextArea area;
        private int hSize = 1;
        private int vSize = 1;
    
        public Expander() {
            Container cp = getContentPane();
            cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
    
            cp.add(Box.createHorizontalGlue());
            area = new JTextArea(vSize, hSize);
            cp.add(area);
            cp.add(Box.createHorizontalGlue());
    
            area.getDocument().addDocumentListener(new DocumentListener() {
    
                @Override
                public void insertUpdate(DocumentEvent e) {
                    adjust();
                }
    
                @Override
                public void removeUpdate(DocumentEvent e) {
                    adjust();
                }
    
                @Override
                public void changedUpdate(DocumentEvent e) {
                    adjust();
                }
            });
    
            pack();
        }
    
        private void adjust() {
            int maxColumns = getMaxColumns();
            if ((area.getLineCount() != vSize) || (maxColumns != hSize)) {
                hSize = maxColumns;
                vSize = area.getLineCount();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        area.setColumns(hSize);
                        area.setRows(vSize);
                        Expander.this.doLayout();
                        Expander.this.pack();
                    }
                });
            }
        }
    
        private int getMaxColumns() {
            int startOffset = 0;
            int maxColumns = 0;
            for (int i = 0; i < area.getLineCount(); i++) {
                try {
                    int endOffset = area.getLineEndOffset(i);
                    int lineSize = endOffset - startOffset;
                    if (lineSize > maxColumns) {
                        maxColumns = lineSize;
                    }
                    startOffset = endOffset;
                } catch (BadLocationException ble) {
                }
            }
    
            return maxColumns;
        }
    
        public static void main(String[] args) {
    
            Expander e = new Expander();
            e.setLocationRelativeTo(null);
            e.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So im trying to find a script that can start me off with creating
I'm trying to create a bookmarklet that will start off an AJAX call to
To start off, this really isn't CodeIgniter specific. I'm having trouble grasping an idea,
I do start off organising my .h files with the best intentions but somehow
I will start off with the code... private static void File() { wixFile =
I start off with a login screen. Then after the user logs in I
I've got a scenario that you can envision this way: Start off with an
Let me start off with a bit of background. This morning one of our
First off, let me start off that I am not a .net developer. The
I start by creating a string variable with some non-ascii utf-8 encoded data on

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.