I have an array of TextAreas. The number of TextAreas is such that not all of them are visible. I would like to add a scroll bar so I can scroll all of them into view. I've tried various ways but none worked.
The following is a code stub that produces the textareas but doesn't show a scrollbar. Appreciate any help. Thanks.
Using a null layout is always a bad idea because then you need to worry about the user resizing the frame or running on a different screen resolution/size.
Always use a proper layout manager. With that, all you need to do is wrap your widget inside a JScrollPane and add the scroll pane to the parent panel.
> BTW. Does JPanel p = new JPanel(null) set the Layout to null the same as p.setLayout(null)?
yes, this is one of the constructors from JPanel's apidocs
public JPanel(LayoutManager layout)Create a new buffered JPanel with the specified layout manager
layout - the LayoutManager to use
to follow up on Maneesh's comment re null layouts, the reason there was no scrollbar would be due to
you setting the scrollpane's layout to null (a null layout makes you responsible for just about everything).
The JScrollBar is a separate component, but when added to a null layout it needs to have a location and a size,
otherwise defaults to 0,0,0,0.
Joined: Dec 27, 2011
Thanks for your comments guys.
The reason I didn't use a Layout Manager was because I wanted the user to be able to resize the individual text areas. I also wanted to write some code so that columns and rows of text areas could also be moved - similar to a spreadsheet.
A GridLayout would work very nicely except column and row movement would be impossible - I think.
I've been putting off working with that GridBag thingy, it looks a bit intimidating and I'm not sure it would allow me to do what I want to do anyway.
Joined: Dec 27, 2011
I took your advice and have been looking at the Grouped Layout Manager. The following code creates a 2 x 2 group of buttons, c0, c1, c2 and c3.
Everything works fine if the buttons are coded individually. However, if I try using a button array (btn) and enter them via a for loop I get a runtime error.
Exception in thread "main" java.lang.IllegalArgumentException: Component must be
Ted Newholm wrote:I've fixed up the initializing problem but I'm still getting a whole pile of runtime errors... Am I doing something wrong with the GroupLayout?
If you're getting runtime errors, then yes, by definition you're doing something wrong. And the error messages are supposed to convey information, you know. I suggest you have a look at them and find out what that information is. That's what I would do. Without that information you're basically guessing in the dark.