• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't Get ScrollBar to Show

 
Ted Newholm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably easier ways, but this seems to work OK.

 
Ted Newholm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael. That worked well.

Much appreciated.

BTW. Does JPanel p = new JPanel(null) set the Layout to null the same as p.setLayout(null)?
 
Maneesh Godbole
Saloon Keeper
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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

JPanel
public JPanel(LayoutManager layout)Create a new buffered JPanel with the specified layout manager

Parameters:
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.
 
Ted Newholm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ted Newholm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[4]) and enter them via a for loop I get a runtime error.

Exception in thread "main" java.lang.IllegalArgumentException: Component must be
non-null
at javax.swing.GroupLayout$ComponentSpring.<init>(GroupLayout.java:2953)
at javax.swing.GroupLayout$ComponentSpring.<init>(GroupLayout.java:2933)
at javax.swing.GroupLayout$Group.addComponent(GroupLayout.java:1524)
at javax.swing.GroupLayout$ParallelGroup.addComponent(GroupLayout.java:2484)
at javax.swing.GroupLayout$ParallelGroup.addComponent(GroupLayout.java:2454)
at javax.swing.GroupLayout$Group.addComponent(GroupLayout.java:1505)
at javax.swing.GroupLayout$ParallelGroup.addComponent(GroupLayout.java:2476)
at GroupedLayout2x2.<init>(GroupedLayout2x2.java:32)
at GroupedLayout2x2.main(GroupedLayout2x2.java:61)

The code is as follows:



Appreciate any help.
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah. You're not initializing all of the array entries, you're leaving nulls in some of them. And then you pass those nulls to the layout, which rightly complains about that.
 
Ted Newholm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
Thanks for reply. I've fixed up the initializing problem but I'm still getting a whole pile of runtime errors.

I now have the following loops:



Am I doing something wrong with the GroupLayout?
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic