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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:23+00:00 2026-05-26T15:30:23+00:00

I have a sample panel which have other component, in my case JTextArea. I

  • 0

I have a sample panel which have other component, in my case JTextArea. I want to increase the height of the panel as the height of JTextArea increases, with fixed width. I have set fixed width for panel.

public class Panel extends JPanel {

    public Panel() {
        setPreferredSize(new Dimension(300, 85));
        setLayout(new BorderLayout());
        JPanel pic = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pic.setBackground(Color.GRAY);
        pic.add(new JLabel(new ImageIcon("img/icon.png")));




        JPanel status = new JPanel(new FlowLayout(FlowLayout.LEFT));
        status.setBackground(Color.GRAY);

        JTextArea textArea = new JTextArea();

        String text = "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog. "
                + "The quick brown fox jumps over the lazy dog.";
        textArea.setText(text);
        textArea.enable(false);

        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        add(textArea);
        status.add(textArea);

        add(pic, BorderLayout.WEST);
        add(status, BorderLayout.CENTER);

        setBorder(BorderFactory.createEtchedBorder());
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Panel Test");

        f.add(new Panel());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

And I don’t want scrollbar for JTextArea. I want it to take as much height as it takes for complete viewing of its content.

  • 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-26T15:30:24+00:00Added an answer on May 26, 2026 at 3:30 pm

    The preferred size of the text area can’t be determined unless the size of text area is known. That is the wrapping of the text can’t be done until the width of the text area is known. So you need to give the text area a width.

    Also, you can’t give the panel a preferred size since this will defeat the purpose of using pack();

    // setPreferredSize(new Dimension(300, 85));
    ...
    textArea.setText(text);
    textArea.setSize(300, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample code, which I expect to color the panel on
Does anyone have sample code, or a tutorial which demonstrates how to use Grappa
I have this sample text, which is retrieved from the class name on an
Does anyone have some sample code or can direct me to which class I
I have a small N large T panel which I am estimating via plm::plm
I have a sample windows service which I got from the following link. http://an-it-solution.blogspot.com/2009/05/how-to-make-windows-service-using-c.html
I have written an panel which supports file / image uploads. So the panel
I have a simple scenario where a panel needs a masked loading indicator over
The situation is very simple, I have two panel. In the event of OnMouseOver
Does anyone have sample code to copy open (in-use and locked by another program)

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.