aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How do I change size of JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I change size of JTextArea" Watch "How do I change size of JTextArea" New topic
Author

How do I change size of JTextArea

Niels Laughlin
Greenhorn

Joined: Apr 04, 2006
Posts: 19
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?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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?)


bitguru blog
Niels Laughlin
Greenhorn

Joined: Apr 04, 2006
Posts: 19
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
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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

Joined: Apr 04, 2006
Posts: 19
Very good answer, this explains exactly what is going on. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I change size of JTextArea
 
Similar Threads
Forcing JTextArea to respect its size
TableLayout Issue
GUI troubles
why repaint is not updating GUI?
Making Typing "Game", any tips or pointers before I start?