• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How do I change size of JTextArea

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to learn how to do things in swing, and I wonder if someone could help me with a component resize problem.

I am using the default JFrame border layout. The North panel contains a JLabel, a JTextField, and a JButton. The Center panel contains a scrollable JTextArea (a JTextArea embedded in a JScrollPane) and nothing else.

Suppose, however, the customer clicks the Maximize button, perhaps because a particular query results in a lot of text. As expected, the window maximizes, the components move around, but nothing changes size. What I would like have happen is for all the components to continue to do this except for the JTextArea. I want it to expand to fill all the available space. In other words, the JTextArea should automatically resize whenever the window is resized.

How do you do this?
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niels Laughlin:
The Center panel contains a scrollable JTextArea (a JTextArea embedded in a JScrollPane) and nothing else.



The center contains the JScrollPane, or the center is the JScrollPane?

If the center is something like a JPanel (under FlowLayout) and you've added the JScrollPane or the JTextArea to that panel, then it will behave as you describe.

You want to add the scroll pane directly to CENTER with no intermediary pane. (Or could add it to CENTER of an intermediary pane under BorderLayout, but what's the point of that?)
 
Niels Laughlin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Brian Cole, your suggestion worked!!! To answer your question, I created a JPanel and put the scroller in it and then put the JPanel in a JFrame. Your suggestion to put the scroller directly in the JFrame worked.

However, I am curious as to why it worked.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Niels Laughlin:
I created a JPanel and put the scroller in it and then put the JPanel in a JFrame. Your suggestion to put the scroller directly in the JFrame worked.

However, I am curious as to why it worked.



When the user enlarges the frame, its BorderLayout enlarges the CENTER component. If the center component is the text area (or a scroll pane containing the text area) then it does what you want.

If you have an intermediate JPanel than it is the panel which will be enlarged. The panel's layout manager then decides if it should resize any of the components it contains. JPanel defaults to FlowLayout, which tries to keep components at their preferred sizes, so no change to the text area's size.
[ December 15, 2006: Message edited by: Brian Cole ]
 
Niels Laughlin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good answer, this explains exactly what is going on. Thanks.
 
And inside of my fortune cookie was this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic