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.
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.
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 ]