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 8088011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:57:52+00:00 2026-06-05T18:57:52+00:00

When I run the sample code below the width of JTextArea is fixed (100px)

  • 0

When I run the sample code below the width of JTextArea is fixed (100px) while its height is dynamically adjusted as I type some text in it.

So for example I start with this:

--------------------
| some text        |
--------------------

and as I type more text the height of JTextArea expands so it fits the content while preserving the width:

--------------------
| some text, some  |
| other longer text|
| etc...           |
--------------------

How can I double the width of JTextArea?
When I do it by changing preferredSize the height is not dynamic anymore.

public class TestTextArea extends JFrame {

    public static void main(String[] args) {
        new TestTextArea().setVisible(true);
    }

    public TestTextArea() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(0,0,800,600);
        setContentPane(createPane());
    }

    protected Container createPane() {
        JTextArea textArea = createTextArea();

        // ------------------------------------------
        // UNCOMMENT TO DOUBLE THE WIDTH OF JTextArea

//        Dimension oldPrefSize = textArea.getPreferredSize();
//        Dimension newPrefSize = new Dimension(oldPrefSize.width * 2, oldPrefSize.height);
//        textArea.setPreferredSize(newPrefSize);

        JPanel pane = new JPanel(new FlowLayout());
        pane.add(textArea);
        return pane;
    }

    protected JTextArea createTextArea() {
        JTextArea textArea = new JTextArea();
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        return textArea;
    }

}
  • 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-06-05T18:57:56+00:00Added an answer on June 5, 2026 at 6:57 pm

    Use the JTextArea#setColumns method to adjust the width

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

Sidebar

Related Questions

Since yesterday, I try to run a sample of code to display a chart
I have the following sample code which doesn't seem to want to run. import
Thanks in advance. This is my sample code. Whenever i try to run it
I'm trying to compile and run the task-android-sample code from Google's API website. I
I have sample code as below import org.codehaus.groovy.control.CompilerConfiguration abstract class MyClass extends Script {
With regards to the sample code below, why is the destructor for the base
When I run this simple code: from pylab import * import numpy as np
I am trying to run the following simple code, public abstract class Shape{ abstract
I am trying to run this seemingly simple piece of code which is repeated
I have a fairly simple block of code. Sub Run(Name) on error resume next

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.