• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Can't Get ScrollBar to Show

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic