This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Can't Get ScrollBar to Show Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can Watch "Can New topic
Author

Can't Get ScrollBar to Show

Ted Newholm
Greenhorn

Joined: Dec 27, 2011
Posts: 10
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

Joined: Jun 09, 2003
Posts: 4632
probably easier ways, but this seems to work OK.

Ted Newholm
Greenhorn

Joined: Dec 27, 2011
Posts: 10
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

Joined: Jul 26, 2007
Posts: 10171
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Dec 27, 2011
Posts: 10
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

Joined: Dec 27, 2011
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Dec 27, 2011
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't Get ScrollBar to Show
 
Similar Threads
JTable inside JScrollPane - background coloring problem
TableModelEvent
Jtable Back ground
Swing--JTable in JInternalFrame
Colors in JTable - unexpected result when scrolling